Candy Sharing Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem 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
分析:按照题意模拟下来就行了,注意在每次循环一开始要判断每个是不是奇数。
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <sstream> 10 #include <queue> 11 #include <typeinfo> 12 #include <fstream> 13 #include <map> 14 #include <stack> 15 using namespace std; 16 #define INF 100000 17 typedef long long ll; 18 const int maxn=10010; 19 int a[maxn],tmp[maxn]; 20 int main() 21 { 22 int n; 23 while(scanf("%d",&n)==1){ 24 if(n==0) break; 25 for(int i=1;i<=n;i++) 26 scanf("%d",&a[i]); 27 int same,ans=0,num; 28 while(1){ 29 same=1; 30 for(int i=1;i<=n;i++) 31 if(a[i]%2!=0) a[i]++; 32 for(int i=2;i<=n;i++) 33 if(a[i]==a[i-1]) {num=a[i];same++;} 34 else break; 35 if(same==n) break; 36 for(int i=1;i<=n;i++){ 37 a[i]=a[i]/2; 38 tmp[i]=a[i]; 39 } 40 for(int i=1;i<=n;i++){ 41 if(i==1) a[i]+=tmp[n]; 42 else a[i]+=tmp[i-1]; 43 } 44 ans++; 45 } 46 printf("%d %d\n",ans,num); 47 } 48 return 0; 49 }