杭电acm 1034题
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.
36
2
2
2
2
2
11
22
20
18
16
14
12
10
8
6
4
2
4
2
4
6
8
0
17 22
4 8
The game ends in a finite number of steps because:
1. The maximum candy count can never increase.
2. The minimum candy count can never decrease.
3. No one with more than the minimum amount will ever decrease to the minimum.
4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.
这道题的大意是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 }