Candy Sharing Game

题目描述

A number of students sit in a circle facing their teacher in the center. Each student initially has an 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.

输入描述:

The input may describe more than one game. For each game, the input begins with the number N of students,followed by N candy counts for the children counter-clockwise around the circle.  Each input number is on a line by itself.

输出描述:

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. 

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

输入

复制

6
36
2
2
2
2
2

输出

复制

15 14

AC_CODE:

#include <stdio.h>
#define N 1000
int count;//吹了几次口哨了
int candy[N];//每个小朋友几颗糖
int n;//有几个小朋友

bool Done()//判断所有小朋友糖数是否相等,是就返回true
{
	for (int i = 1; i<n; i++)
	{
		if (candy[i] != candy[0]) return false;
	}
	return true;
}
void BlowWhistle()
{
	int share[N];//每个人打算分给右边的数量
	for (int i = 0; i<n; i++) share[i] = candy[i] / 2;
	for (int i = 0; i<n - 1; i++)//先处理前n-1个小朋友
	{
		candy[i] -= share[i];
		candy[i + 1] += share[i];
	}
	candy[n - 1] -= share[n - 1];
	candy[0] += share[n - 1];//最后一个小朋友发糖给第一个小朋友
	for (int i = 0; i<n; i++)//老师补发糖果
	{
		if (candy[i] % 2 == 1) candy[i]++;
	}
	count++;//口哨计数器+1
}
int main()
{
	while (scanf("%d", &n) != EOF)
	{
		if (n<0) break;//负数个小朋友,退出程序
		else if (n == 0)//0个小朋友的话
		{
			printf("0 0\n");
			continue;
		}
		for (int i = 0; i<n; i++) scanf("%d", &candy[i]);//输入
		while (!Done()) BlowWhistle();//糖数不一样就一直吹
		printf("%d %d\n", count, candy[0]);//输出结果
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值