Water Bills

Description


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,分析对程序运行效率的重要性可见一斑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接口定义: ``` GET /user/bills?username={username} ``` 请求参数: - `username`: 用户名,必填 响应数据: - `electric_bill`: 电费金额,单位为元,可能为空 - `water_bill`: 水费金额,单位为元,可能为空 - `start_date`: 账单起始日期,格式为 `yyyy-MM-dd` - `end_date`: 账单结束日期,格式为 `yyyy-MM-dd` 可能的异常: - `400 Bad Request`: 请求参数不合法 - `404 Not Found`: 用户不存在 - `500 Internal Server Error`: 服务器内部错误 实现代码(Python Flask框架): ```python from flask import Flask, jsonify, request app = Flask(__name__) # 模拟数据库查询电费和水费账单的函数 def query_bills(username: str): # TODO: 根据用户名查询数据库,获取当月水费和电费账单信息 electric_bill = None water_bill = None start_date = '2021-08-01' end_date = '2021-08-31' return electric_bill, water_bill, start_date, end_date @app.route('/user/bills') def get_user_bills(): # 获取用户名参数 username = request.args.get('username') if not username: return jsonify({'message': '用户名不能为空'}), 400 # 查询账单信息 electric_bill, water_bill, start_date, end_date = query_bills(username) if not electric_bill and not water_bill: return jsonify({'message': '该用户本月没有水电费账单'}), 404 # 返回账单信息 result = { 'electric_bill': electric_bill, 'water_bill': water_bill, 'start_date': start_date, 'end_date': end_date, } return jsonify(result), 200 if __name__ == '__main__': app.run() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值