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;
}