现在有一个功能,有一个lst是从XML里面读取出来的,lst的大小不定,里面有 三个主要的字段 ,startTime,endTime,price
startTime,endTime,price
0        ,30     ,0.5     
30       ,60     ,0.4   
60       ,120    ,0.3  
120      ,180    ,0.2  
180      ,0      ,0.1例如 是这样的一个集合,现在要求 传入一个时间 ,来算取消费总额,请问 用递归怎么做呢     递归xml

解决方案 »

  1.   

    和递归没有关系
    大概思路:
    list.Where(x => x.startTime >= 你的时间 && x.endTime <= 你的时间).Sum(x => x.price)
      

  2.   

    递归模型:
    设传入时间为 t
    1 设 本消费等级单价 函数 getPrice(t)
    2 设 本消费等级时间 函数 getTime(t);
    3 设 本消费等级金额 函数 getAmount(t){return getPrice(t)*getTime(t)};
    4 设 计算完本级消费金额后剩余时间计算函数 getLeftTime(t);function 消费总额(t){
    if(t==0){return 0};return 消费总额(getLeftTime(t))+getAmount(t);
    }完成以上函数,程序就出来了
      

  3.   

    看样子貌似是分时段求和,需求都说不清楚
    private float Calcute(int minutes)
    {
        int[] times = { 0, 30, 60, 120, 180 };
        float[] prices = { 0.5f, 0.4f, 0.3f, 0.2f, 0.1f };
        float res = 0f;
        for (int i = times.Length - 1; i >=0; i--)
        {
            if (minutes >= times[i])
            {
                res += (minutes - times[i]) * prices[i];
                minutes = times[i];
            }
        }
        return res;
    }