Candy sharing Game
Topic linksCandy sharing Game
Main Topic
That is, there are n small students, each classmate has a certain number of candies, now press counter-clockwise to stand a ring, every time the teacher whistled, each child will put the candy in the hand half to the right of the children, if he is in his hand is odd number of sweets, he will get a candy from the teacher, ask now need to blow a few rounds And finally the number of sweets in each child's hand. Solving
Originally thought can launch a magic mathematical formula, the result simulation is over ... Code
#include <iostream> #include <cstdio> #include <cstring> using namespace
Std
int a[2][1000005],p,cnt,ans,n;
BOOL Check (int k) {for (int i=1;i<n;i++) if (a[k][i]!=a[k][0]) return 0;
return 1;
} void Solve (int k) {for (int i=1;i<=n;i++) {a[k%2][i%n]=a[(k+1)%2][i%n]/2+a[(k+1)%2][(i-1)%N]/2;
if (a[k%2][i%n]%2) {a[k%2][i%n]++;
ans++;
}}} int main () {while (scanf ("%d", &n), n!=0) {memset (a,0,sizeof (a)); cnt=0;
P=0;
for (int i=0;i<n;i++) scanf ("%d", &a[0][i]);
while (!check (p%2)) {solve (++p);
cnt++;
} printf ("%d%d\n", cnt,a[p%2][0]);
} return 0; }