Description
Input
兩個整數N和K.
Output
最後的牛羣數.
Sample Input
INPUT DETAILS:
There are 6 cows and the difference in group sizes is 2.
Sample Output
OUTPUT DETAILS:
There are 3 final groups (with 2, 1, and 3 cows in them).
6
/ \
2 4
/ \
1 3
HINT
6只奶牛先分成2只和4只.4只奶牛又分成1只和3只.最後有三羣奶牛.
題解:
直接模擬就好,沒什麼好說的吧……隊列實現,或者遞歸實現,我用的隊列
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
queue <int > Q;
int main(int argc, char *argv[])
{
int n,k,t,d=0;
scanf("%d%d",&n,&k);
Q.push(n);
while(!Q.empty())
{
t=Q.front();
if(t>k&&(t-k)%2==0)
Q.push((t-k)/2),Q.push((t-k)/2+k);
else d++;
Q.pop();
}
printf("%d\n",d);
return 0;
}