Peter’s smokes
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 2019, Accepted users: 1902
Problem 10082 : No special judgement
Problem description
Peter has n cigarettes. He smokes them one by one keeping all the butts. Out of k > 1 butts he can roll a new igarette.
How many cigarettes can Peter have?
Input
Input is a sequence of lines. Each line contains two integer numbers giving the values of n and k.
Output
For each line of input, output one integer number on a separate line giving the maximum number of cigarettes that Peter can have.
Sample Input
4 3
10 3
100 5
Sample Output
5
14
124
分析
这个题的坑就是需要考虑除去能形成新的烟的剩余烟头是否能和新形成的烟组成新的烟;
left表示商
yu表示余数
temp是储存上一次的余数和商的和
sum储存烟头
代码
#include<iostream>
using namespace std;
int main() {
int n,m;
while(cin>>n>>m) {
int sum=n,temp,left=n,yu=0;
while(1) {
temp=left+yu;
left=temp/m;
if(left==0)//如果剩余烟头不满足能形成新的烟,就跳出循环
break;
yu=temp%m;
sum+=left;
}
cout<<sum<<'\n';
}
}