Candy Sharing Game【C语言】分糖果问题

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.

有多组输入数据。每一组,输入一个数N代表学生人数,之后输入N个数据(N为偶数)为孩子们的初始糖果数,孩子们逆时针围成一个圈。0表示输入结束。每个数据单独为一行输入。

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.

游戏以有限的步骤结束,因为:
1.糖果的最大数量永远不会增加。
2.最小的糖果数量永远不会减少。
3.任何超过最低限额的人都不会减到最低限额。
4.如果糖果的最大和最小数量不相同,至少有一个学生的最小数量必须增加。

思路
思路其实在认真翻译完后,大概就会清楚,只是写成程序会有些麻烦
得把糖果数变成原来的一半,在按照规律相加,但是计算下一个是,原来的数据改变了,计算结果会出错,于是得把原来的数据先存储下来。
code

#include<stdio.h>
#include<string.h>
int Not_aver(int a[],int n);//用于判断每个学生的糖果数是否相等,即游戏结束的条件。
int Is_jishu(int x);//判断学生的糖果数是否为奇数,是奇数就加1;
int main()
{
	int a[1000]={0},n;int temp,tmp,i;
	while(scanf("%d",&n)&&n!=0){
		int count=0;
		for(i=0;i<n;i++)	scanf("%d",&a[i]);
		while(Not_aver(a,n)){
			for(i=0;i<n;i++)	a[i]=a[i]/2;//把糖果数减半
			for(i=0;i<n;i++){
				if(i==0){//第一个学生拿的是最后一个学生的糖果
					temp=a[i];//暂时储存当前的糖果数
					tmp=a[i+1];//暂时储存下一个位置的糖果数
					a[i]=a[i]+a[n-1];
					a[i]=Is_jishu(a[i]);
				}
				else{
					a[i]=temp+tmp;//从第二个学生开始可以通过暂时存储的数据求得糖果数
					a[i]=Is_jishu(a[i]);
					temp=tmp;//将temp下移
					tmp=a[i+1];//将tmp下移
				}
			}
			count++;//每完成一次交换,计数器加一
		}
		printf("%d %d\n",count,a[0]);
		count=0;memset(a,0,sizeof(a));//对计数器和数组清零
	}
} 
int Not_aver(int a[],int n)
{
	int i;
	for(i=0;i<n-1;i++){
		if(a[i]!=a[i+1])	return true;//有一组不相等,就返回真,游戏继续
	}
	return false;
}
int Is_jishu(int x)
{
	if(x%2==0) return x;
	else		return x+1;
}

感悟:学好英语很重要,一开始我就是真•题都看不懂,这些翻译都是我自己翻译的,不是机翻,仅供参考。

原创文章 27 获赞 26 访问量 7777

猜你喜欢

转载自blog.csdn.net/unseven/article/details/105518644
今日推荐