1003 Exchange for Cola

1003 Exchange for Cola

Exchange for Cola

时间限制: 1秒  内存限制: 64M

Problem Description

The Cola company is holding a promotional activity that customerscan exchange B bottles of Cola with A caps. Now Silence has N caps. Furthermore, Silence can ask mm to borrow him some caps for anytimes as long as Silence is able to return the caps borrowed after he has used these caps to exchange Cola without buying any other Cola. Please tell Silence how many number of Cola Silence can drink.

Input

This problem contains multiple test cases. Each case contains oneline with three numbers N , A , B ,(1 <= N <= 10^9, 1 <= B < A <= 10^9), which have already been mentioned above.

Output

Print exactly one line with the maximal number of Cola Silence can drink by exchanging caps for each test case.

Sample Input

1 2 1

9 7 2

Sample Output

1

2


步骤:

1、将手中已有瓶盖全部兑换成可乐

2、将已有可乐全部喝掉(别撑着,喝不了给我),将可乐换成瓶盖

3、返回步骤 1 直到手中瓶盖不够继续兑换

4、找mm借瓶盖,算算是否能还得上。如果还得上,转步骤 5 ,否则转步骤 7 

5、将瓶盖借来,将所有瓶盖兑换成可乐,将已有可乐全部喝掉,将可乐换成瓶盖(要是喝不了就把瓶盖拧下来)

6、返回步骤 4 

7、告诉我你一共喝了几瓶可乐(剩下的瓶盖自己弹着玩)


要点:

在内存中要注意除法的使用,对于不同数据,b * ( n / a )和b * n / a 计算结果是不同的
当 n == 45, a == 12, b == 15 时,前者的值为39,后者的值为46,因此造成了无限循环而导致超时

要注意对于因整型数据除法的小数部分省略的习惯造成的表达式的括号的误省略问题


  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4.     long n,a,b;  
  5.     long bottle,left;  
  6.     while( scanf("%d%d%d",&n,&a,&b) != -1 )  
  7.     {  
  8.         bottle = 0;  
  9.         long i = n;  
  10.         while ( i / a != 0 )   
  11.         {  
  12.             left = i % a;  
  13.             bottle += b * ( i / a );//要点中说的就是这里 
  14.             i = b * ( i / a ) + left;  
  15.         }  
  16.         while ( b >= a - i )  
  17.         {  
  18.             i = b - ( a - i );  
  19.             bottle += b;  
  20.         }  
  21.         printf("%d\n",bottle);  
  22.     }  
  23.     return 0;  
  24. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值