Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
15 14 17 22 4 8
Hint
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.
这是我的代码,我依旧不知道这是为什么!!!! 被自己蠢死了!
#include <iostream> using namespace std; int x[1000],y[1000]; void f(int s[],int n,int k) { int i,j,a[100][100]; for(i=0;i<n;i++)a[0][i]=s[i]; for(j=1;;j++){ bool flag=true; a[j][0]=a[j-1][0]/2+a[j-1][n-1]/2; if(a[j][0]%2!=0)a[j][0]++; for(i=1;i<n;i++){ a[j][i]=a[j-1][i-1]/2+a[j-1][i]/2; if(a[j][i]%2!=0)a[j][i]++; if(flag==true&&a[j][i]!=a[j][0])flag=false; } if(flag==true){ x[k]=j; y[k]=a[j][0]; break; } } } int main() { int n,k=0; while(cin>>n){ if(n==0)break; int s[100],i; for(i=0;i<n;i++)cin>>s[i]; f(s,n,k); k++; } for(int i=0;i<k;i++) cout<<x[i]<<" "<<y[i]<<endl; //system("pause"); return 0; }
这是正确代码
#include<iostream> using namespace std; const int MAXN=1000; int a[MAXN]; int main() { int n; int i; while(cin>>n,n) { for(i=0;i<n;i++)cin>>a[i]; int res=0; while(1) { for(i=1;i<n;i++) if(a[i-1]!=a[i]) break; if(i>=n) break; res++; int temp=a[n-1]/2; for(i=n-1;i>0;i--) { a[i]/=2; a[i]+=a[i-1]/2; } a[0]/=2; a[0]+=temp; for(i=0;i<n;i++) if(a[i]&1) a[i]++; } cout<<res<<" "<<a[0]<<endl; } //system ("pause"); return 0; }
本文介绍了一种学生间传递糖果的游戏算法,通过轮流传递并调整糖果数量直至每位学生手中的糖果数相同。文中提供了两种实现该算法的C++代码示例,并分析了游戏结束条件。
272

被折叠的 条评论
为什么被折叠?



