原题描述如下
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.
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 8HintThe 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个 小朋友围成一个圈,每个小朋友手上有a [ i ] (初始值为偶数)个糖果,一次循环内,把自己一半的糖果给自己右手边的人,给完和收到之后如果自己的糖果数是奇数,老师会给他一个补成偶数个,知道所有的小朋友手上的糖果数目相等。
思路: 用两个数组a,b分别表示小朋友手上的糖果数目和要给别人的糖果数目,第i个小朋友自己的一半加左边给的一半就是一次循环后的拥有的糖果数目,依次类推。
#include <stdio.h>
int main( )
{
int i, n, round = 0;
while(scanf("%d", &n) != EOF && n)
{
int a[100];
int b[100];
int flag = 1;
for ( i = 0; i < n; i++)
scanf("%d",&a [ i ] ); //数组a用来表示i个学生手上有多少个苹果
while ( flag )
{
flag = 0;
for ( i = 1; i < n; i++ ) //用来判断是否所有学生手上的数目相等
if ( a [ i ] != a [ 0 ] )
{
flag = 1; //表示所有学生的糖果数目不等
break;
}
if ( i == n )
break;
for ( i = 0; i < n; i++ ) //开始分配
{
a [ i ] = a [ i ] / 2;
b[ i ] = a [ i ] ;
}
for ( i= 0; i < n ; i++)
{
if ( i == 0 ) //第一个小孩由最后一个给
a [ i ] = a [ i ] + b [ n-1 ];
else
a [ i ] = a [ i ] + b [ i-1 ];
}
for ( i = 0; i < n; i++) //分配完之后如果是奇数就加一
{
if ( a [ i ] % 2 != 0 )
a [ i ] ++;
}
round ++;
}
printf("%d %d\n",round,a[ 0 ]);
}
return 0;
}
不过提交结果是时间超限。。。。。。是因为开了两个数组又用了太多for循环吗。。。。。
然后精简了一下代码,发现其实flag不要也可以。。。把判断是否为奇数的循环直接放到分配循环里这样就省掉了一次循环。同时不用数组,动态调用内存。
#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main( )
{
int i, n,round = 0;
while(scanf("%d", &n) != EOF && n)
{
int * a = (int *)malloc(n * sizeof(int));
int * b = (int *)malloc(n * sizeof(int));
for ( i = 0; i < n; i++)
scanf("%d",&a [ i ] ); //数组a用来表示i个学生手上有多少个苹果
while ( 1 )
{
for ( i = 1; i < n; i++ ) //用来判断是否所有学生手上的数目相等
if ( a [ i ] != a [ 0 ] ) //表示所有学生的糖果数目不等
break;
if ( i == n )
break;
for ( i = 0; i < n; i++ ) //开始分配
{
a [ i ] = a [ i ] / 2;
b[ i ] = a [ i ] ;
}
for ( i= 0; i < n ; i++)
{
if ( i == 0 ) //第一个小孩由最后一个给
a [ i ] = a [ i ] + b [ n-1 ];
else
a [ i ] = a [ i ] + b [ i-1 ];
if ( a [ i ] % 2 != 0 )
a [ i ] ++;
}
round ++;
}
printf("%d %d\n",round,a[0]);
free( a );
free ( b );
}
return 0;
}
结果。。。WA了。。。因为round的定义放在最外面了。。。。。
#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main( )
{
int i, n;
while(scanf("%d", &n) != EOF && n)
{
int round = 0;
int * a = (int *)malloc(n * sizeof(int));
int * b = (int *)malloc(n * sizeof(int));
for ( i = 0; i < n; i++)
scanf("%d",&a [ i ] ); //数组a用来表示i个学生手上有多少个苹果
while ( 1 )
{
for ( i = 1; i < n; i++ ) //用来判断是否所有学生手上的数目相等
if ( a [ i ] != a [ 0 ] ) //表示所有学生的糖果数目不等
break;
if ( i == n )
break;
for ( i = 0; i < n; i++ ) //开始分配
{
a [ i ] = a [ i ] / 2;
b[ i ] = a [ i ] ;
}
for ( i= 0; i < n ; i++)
{
if ( i == 0 ) //第一个小孩由最后一个给
a [ i ] = a [ i ] + b [ n-1 ];
else
a [ i ] = a [ i ] + b [ i-1 ];
if ( a [ i ] % 2 != 0 )
a [ i ] ++;
}
round ++;
}
printf("%d %d\n",round,a[0]);
free( a );
free ( b );
}
return 0;
}
以上。