UVa 10346 – Peter’s Smokes
Peter has N cigarettes, and he smokes them one by one, saving the leftover cigarette. When he accumulates K leftover cigarettes (where K > 1), he can roll them into a new cigarette.
How many cigarettes can Peter smoke in total?
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF inputs: each test case consists of one line. Each line contains two integers, N and K. | For each input line, please output the total number of cigarettes Peter can smoke. |
4 3 10 3 100 5 | 5 14 124 |
Thought Process
Use a While loop where after each cigarette is smoked, the number of leftover cigarettes increases by 1. Check if the current number of leftover cigarettes can be rolled into a new cigarette. If "there are no cigarettes left" && "the number of leftover cigarettes is less than K", then terminate the loop and output the answer.
Sample Code-ZeroJudge C079: Peter’s Smokes
#include <iostream>
using namespace std;
int main() {
int N, K;
while (cin >> N >> K)
{
int ans = 0, count = 0;
while (true)
{
if (N > 0)
{
ans++;
count++;
N--;
}
else if (count >= K)
{
ans++;
count-=K;
count++;
}
else break;
}
cout << ans << endl;
}
}
//ZeroJudge C079
//Dr. SeanXD