Water Bills
来源:互联网 发布:北邮程序员被老婆害死 编辑:程序博客网 时间:2024/06/13 00:57
It's 30 century. And water has become very expensive. Recently, your water company raised the rates once more. The table below shows the new rates (consumption is always a positive integer):
Range Price
(cm3) (yuan)
1~100 2
101~10000 3
10001~1000000 5
>1000000 7
This means that, when calculating the amount to pay, the first 100 cm3 have a price of 2 yuan each; the next 9900 cm3 (between 101 and 10000) have a price of 3 Americus each and so on.
For instance, if you use 10123 cm3 water you will have to pay 2 x 100 + 3 x 9900 + 5 x 123 = 30515 yuan.
The evil mathematicians from the company have found a way to gain even more money. Instead of telling you how much energy you have consumed and how much you have to pay, they will show you two numbers related to yourself and to a random neighbor:
A: the total amount to pay if your consumptions were billed together;
B: the absolute value of the difference between the amounts of your bills.
If you can't figure out how much you have to pay, you must pay another 100 yuan for such a "service". You are very economical, and therefore you are sure you cannot possibly consume more than any of your neighbors. So, being smart, you know you can compute how much you have to pay.
Input
The input contains several test cases. Each test case has a single line, containing two integers A and B, separated by a single space, representing the numbers shown to you (1<=A, B<=10^9). You may assume there is always a unique solution, that is, there exists exactly one pair of consumptions that produces those numbers.
The last test case is followed by a line containing two zeros separated by a single space.
Output
For each test case in the input, your program must print a single line containing one integer, representing the amount you have to pay.
Sample Input
1100 300
35515 27615
0 0
Sample Output
350
2900
这个大体思路很明确,就是根据bill捆绑以后的费用计算二者的用水总量,然后在这个范围内找到各自的用水量以满足二者的费用差符合要求即可。一开始做的时候没有考虑时间的问题,用了几乎遍历半数的搜索,导致最后程序用时达260ms之多,分析知可以通过二分查找节约时间,所以讲程序修改为:
#include<stdlib.h>
#include<stdio.h>
int use(int m)//根据输入的水费返回用水量在哪个范围内
{
if(m > 4979900)
return 4;
if(m > 29900)
return 3;
if(m > 200)
return 2;
return 1;
}
int computer(int a)//根据输入的用水量返回水费
{
int price;
price = 0;
if(a > 1000000)
{
price += ((a - 1000000)*7);
a = 1000000;
}
if(a > 10000)
{
price += ((a-10000)*5);
a = 10000;
}
if(a > 100)
{
price += ((a - 100)*3);
a = 100;
}
price += (a * 2);
a = 0;
return price;
}
int main()
{
int m,n,count,level,i,j;
while(scanf("%d %d",&m,&n)&&(m != 0||n !=0))
{
level = use(m);
//计算二者绑定账单后的用水量
switch(level){
case 1:{count = m/2;break;}
case 2:{count = ((m-200)/3)+100;break;}
case 3:{count = ((m-29900)/5)+10000;break;}
case 4:{count = ((m-4979900)/7)+1000000;break;}
default:break;
}
//开始二分查找各自的用水量
i=0;j=count;
while(i<j)//此处无需考虑没有结果的情况
{
if(computer(count - ((i + j)/2))-computer((i+j)/2)>n)
i=((i+j)/2)+1;
else
{
if(computer(count - ((i + j)/2))-computer((i+j)/2)<n)
j=((i+j)/2)-1;
else
break;
}
}
printf("%d\n",computer((i+j)/2));//输出结果
}
return 0;
}
改后用时9ms,分析对程序运行效率的重要性可见一斑。
- Water Bills
- Bills
- Phone Bills
- Phone Bills
- Phone Bills
- Water
- WATER
- Water+
- water
- pat1016 Phone Bills
- 1016. Phone Bills (25)
- PAT_1016: Phone Bills
- 1016 Phone Bills (25)
- PAT 1016 Phone Bills
- PAT 1016. Phone Bills
- 1016. Phone Bills
- PAT1016. Phone Bills
- PAT1016. Phone Bills
- 03 ActionWildcard_通配符配置02
- OpenCV视频目标跟踪示例教程(Meanshift)
- myeclipse 8.5 +tomcat 6.x+jdk 搭建 jsp 开发环境简记
- 对话框控件上创建窗口
- 关于win7 x64 连接oracle 客户端 VS2010调试 提示“ORA-12154: TNS: 无法解析指定的连接标识符 ”
- Water Bills
- python中if __name__ == '__main__': 的解析
- 用dw设计网页 “设计”视图无法编辑
- 浏览器工作原理
- Unity3D研究院之IOS实现平面多点触摸(二)
- three.js入门
- Using R to Fix Data Quality: Section 0
- 使用DD_belatedPNG让IE6支持PNG透明图片 及IE6下背景图闪烁问题的解决方法
- number(p,s)