# 求"无限级"阶梯电费的算法

0

（阶梯电费以一年为一个周期）。

Laputa1984 | 初学一级 | 园豆：64

0

Daniel Cai | 园豆：10424 (专家六级) | 2017-01-18 09:02

@Way_1984: 你都没规律，写个配置文件老老实实的匹配吧。

0

2
``` 1 #include <stdio.h>
2
3 double FeeCaculate(double used_spare,double the_spare,int n,double steps[],double prices[])
4 {
5     int i;
6     int start_step=-1,end_step=-1;
7     double the_money=0;
8     for(i = 0;i < n;i++)
9     {
10         if(steps[i] >= used_spare && start_step < 0)
11         {
12             start_step = i;
13         }
14         if(steps[i] > used_spare+the_spare)
15         {
16             end_step = i;
17             break;
18         }
19     }
20     if(end_step < 0)
21     {
22         end_step = n-1;
23     }
24     the_money += prices[start_step]*(steps[start_step]-used_spare);
25     for(i = start_step+1;i < end_step;i++)
26     {
27         the_money += prices[i]*(steps[i+1]-steps[i]);
28     }
29     the_money += prices[end_step]*(used_spare+the_spare-steps[end_step-1]);
30     return the_money;
31 }
32
33 int main()
34 {
35     double UsedSpare = 0.0;
36     double TheSpare = 1.5;
37     double TheMoney=0.0;
38     int n = 10;
39     double steps[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
40     double prices[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
41     TheMoney = FeeCaculate(UsedSpare, TheSpare, n, steps,prices);
42     printf("The money=%lf\n",TheMoney);
43     return 0;
44 }```

C4ISR | 园豆：214 (菜鸟二级) | 2017-01-18 16:48
0

0

您需要登录以后才能回答，未注册用户请先注册