A number of students sit in a circle facing their
teacher in the center. Each student initially has an even number of pieces of
candy. When the teacher blows a whistle, each student simultaneously gives half
of his or her candy to the neighbor on the right. Any student, who ends up with
an odd number of pieces of candy, is given another piece by the teacher. The
game ends when all students have the same number of pieces of
candy.
Write a program which determines the number of times the teacher
blows the whistle and the final number of pieces of candy for each student from
the amount of candy each child starts with.
The input may describe more than one game. For each
game, the input begins with the number N of students, followed by N (even) candy
counts for the children counter-clockwise around the circle. The input ends with
a student count of 0. Each input number is on a line by itself.
For each game, output the number of rounds of the
game followed by the amount of candy each child ends up with, both on one
line.
6 36 2 2 2 2 2 11 22 20 18 16 14 12 10 8 6 4 2 4 2 4 6 8 0
这道题的大意是N个开始都拿着偶数颗糖的小朋友围着老师坐,老师一吹哨子同时给自己右手边的同学自己一半的糖,结束后奇数颗糖的同学老师会给一颗糖补成偶数棵,直到大家手上的糖一样数目...问需要玩几次,以及最后糖的数量是多少?
1 #include <iostream>
2 using namespace std;
3 #define Max 10000
4 int judge(int *stu,int len);
5 int main(void)
6 {
7 int student[Max];
8 int N;
9 int j=0;
10 int i=0;
11 int temp[Max];
12 int around=0;
13 cin>>N;
14 while(N)
15 {
16 around=0;
17 for(i=0;i<N;i++)
18 cin>>student[i];
19 for(j=0;;j++)
20 {
21
22 //所有的都看看有没有奇数
23 for(i=0;i<N;i++)
24 {
25 student[i]=student[i]/2;
26 temp[i]=student[i];
27 }
28 for(i=1;i<N;i++)
29 {
30 student[i]+=temp[i-1];
31 }
32 student[0]+=temp[N-1];
33 for(i=0;i<N;i++)
34 {
35 if(student[i]%2==1)
36 student[i]+=1;
37 }
38 around+=1;
39 if(judge(student,N))
40 break;
41
42 }
43
44 cout<<around<<" "<<student[0]<<endl;
45 cin>>N;
46 }
47 return 0;
48 }
49 int judge(int *stu,int len)
50 {
51 int flag=1;
52 for(int i=1;i<len;i++)
53 {
54 if(stu[i-1]==stu[i])
55 continue;
56 else {flag=0;break;}
57
58 }
59 return flag;
60
61 }