首页 > 试题广场 >

Candy Sharing Game

[编程题]Candy Sharing Game
  • 热度指数:3986 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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
11
22
20
18
16
14
12
10
8
6
4
2
4
2
4
6
8
0

输出

15 14
17 22
4 8
头像 宁静的冬日
发表于 2022-03-07 11:11:13
题目有坑 如果初始小朋友的糖果为奇数的话,小朋友给同学n/2个糖果,自己留n/2+1个糖果 #include<iostream> #include<vector> #include<algorithm> #define MAX 10000 using namesp 展开全文
头像 wbc990512
发表于 2021-01-26 18:09:27
题意:n个学生坐成一个圆圈,每人初始有一些糖果。老师吹一次口哨,每个学生同时把ta的一半糖果给右边的学生。结束时如果有学生糖果为奇数,则补一颗。当所有学生都有相同数量的糖果时,游戏结束。模拟游戏,确定老师吹口哨的次数,和最终学生的糖果数。 输入:每一组数据先输入n,接下来的n行表示逆时针方向每个 展开全文
头像 Shawn698
发表于 2024-03-18 18:05:01
#include <iostream> using namespace std; bool is_equal(int* x, int n) { for (int i = 0; i < n - 1; ++i) { if (!(x[i] == x[i + 1]) 展开全文
头像 lyw菌
发表于 2023-03-07 20:11:04
//每次循环交换时,若手中糖果为奇数,则多的那一个留给自己。题比较简单,但是题意没有讲清楚 #include "stdio.h" #include "queue" using namespace std; int student[1000];//记录学生的糖果数,从student[1]开始 bool 展开全文
头像 健康快乐最重要
发表于 2020-03-03 17:21:16
注意:初试输入时有奇数。如果有奇数,自己留一半+1,给别人一半。偶数正常。 #include<iostream> #include<vector> using namespace std; const int maxn=10001; int a[maxn]; int t[ma 展开全文

问题信息

难度:
33条回答 5169浏览

热门推荐

通过挑战的用户

查看代码