里程(L/km) 费用(元)
35<L≤200 106
200<L≤400 151
400<L≤600 227
600<L≤800 275
800<L≤1000 376
1000<L≤1200 416
1200<L≤1400 455
1400<L≤1600 496
1600<L≤1800 534
1800<L≤2000 568
2000<L≤2200 601
2200<L≤2400 688
2400<L≤2600 724
2600<L≤2800 757
2800<L≤3000 784
3000<L≤3200 868
3200<L≤3400 903
3400<L≤3600 928
3600<L≤3800 964
3800<L≤4000 1042
4000<L≤4200 1071
4200<L≤4400 1095
L >4400km 时,每 增加 200km 增加 73我要怎么根据录入的里程数计算出费用,大家有没有好的算法,谢谢!

解决方案 »

  1.   

    是这样我现在不管是放表里还是直接写语句都是想的要么循环要么if,case什么的,有没有更好的方法啊,比如数组,然后怎么建,怎么循环呢,大家有没好的处理方法
      

  2.   

    数组就是一种表呀
    int[] t = new int[]{106,151,227,376,416,......,1095};
    int r;
    if(L<= 4400 && L>35)
    {
           r = t[(L-1)/200];
    }
    else
    {
           r = 1095 + ((L-4400)/200)*73;
    }
    大概就是这样字
      

  3.   

    //如果判断的标准是固定,这样写就可以了
        public int  CalValue(int value)
        {
        
            if (value > 35 && value < 200)
            {
                return 106;
            }
            else if (value > 200 && value < 400)
            {
                return 151;
            }
              ......
            else if (value > 4400)
            {
                return ((value - 4400) / 200) * 73 + 1095;
            }
            return 0;
     
        }
      

  4.   

    纯用数据库的话,将所有分界点值存入数据库T表
    lower upper value step
    =================
    35    200   106   null
    200   400   151   null
    ........
    4200  4400  1095  null
    4400  null  73    200
    然后后用一个sql查出来--declare @L --你要查的值
    select 
      case 
       when value is null then null
       when step is null then value
       else isnull(value, 
        (select value from T where upper=obj.lower) 
          + cast((@L - lower)/step as int) * value) 
       end
    from T obj
    where lower<@L and isnull(upper, 1000000)>=@L
    只要算法不变,边界变动时维护数据即可。
    当然,这里举个例,不美观,建议逻辑写在程序里。
      

  5.   

    class Program
        {
            static void Main(string[] args)
            {
                int L = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(GetResult(L));
                Console.Read();
            }
            static string GetResult(int L)
            {
                int[] array = { 106,151, 227, 275, 376,…… 1095 };
                int money = 0;
                if (35 < L && L <= 4400)
                {
                    int i =( L -1)/ 200;
                    money = array[i];
                }
                if (L > 4400)
                {
                    money=((L - 4400) / 200) * 73+1095;
                }
                return money.ToString();
            }
        }
      

  6.   

    还有一种,我经常用的,用Hashtable。这个要using System.Collections;
     Hashtable mytable = new Hashtable();
                mytable.Add(1, 106);
                mytable.Add(2, 151);
                //...
                mytable.Add(22, 1095);
                int miles = 300;
                int cost = mytable[Math.Ceiling(miles/ 200)];
      

  7.   


                楼主可以这样,将每个分段的最大值当作键,费用当作值。保存到字典当中。用字典当中的Key和里程判断,计算相关费用
                Dictionary<int, int> dict = new Dictionary<int, int>();
                dict.Add(200, 400);
                dict.Add(400, 151);
                //
                dict.Add(4400,1095);
      

  8.   

    可能我没有表述清楚,我是想不用IF ELSE,CASE;网上看了看说用设计模式来代替这个,不知有没必要用设计模式,要是用这种情况用那种合适。先谢谢大家。我结贴,再开新帖问吧。