题目1145:Candy Sharing Game
来源:互联网 发布:拳皇98出招优化 编辑:程序博客网 时间:2024/05/16 00:42
- 题目描述:
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.
- 输入:
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.
- 输出:
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.
- 样例输入:
6362222211222018161412108642424680
- 样例输出:
15 1417 224 8
- 提示:
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
好好读懂题意即可。
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define MAX 100int buf[MAX];int half[MAX];int main() {int n;while (cin >> n) {if (n == 0)break;for (int i = 0; i < n; i++) {scanf("%d", &buf[i]);half[i] = buf[i] / 2;}int cnt = 0, candy = buf[0];bool flag = true;for (int i = 0; i < n - 1; i++)if (buf[i] != buf[i + 1]) {flag = true;break;} elseflag = false;while (flag) {for (int i = 1; i <= n; i++) {buf[i % n] = half[(i - 1) % n] + half[i % n];}for (int i = 0; i < n; i++) {if (buf[i] % 2 != 0)buf[i]++;half[i] = buf[i] / 2;}cnt++;candy = buf[0];for (int i = 0; i < n - 1; i++)if (buf[i] != buf[i + 1]) {flag = true;break;} elseflag = false;}printf("%d %d\n", cnt, candy);}return 0;}
题目链接:
http://ac.jobdu.com/problem.php?pid=1145
- 题目1145:Candy Sharing Game
- 题目1145:Candy Sharing Game
- 题目1145:Candy Sharing Game
- 题目1145:Candy Sharing Game
- 模拟_题目1145:Candy Sharing Game
- 1052. Candy Sharing Game
- Candy Sharing Game
- 1034:Candy Sharing Game
- 1034 Candy Sharing Game
- 1052. Candy Sharing Game
- hdu1034-Candy Sharing Game
- hdu1034 Candy Sharing Game
- Candy Sharing Game
- POJ1666(Candy Sharing Game)
- sicily1052 Candy Sharing Game
- Sicily: Candy Sharing Game
- Candy Sharing Game
- Candy Sharing Game
- Java调用phantomjs采集ajax加载生成的网页
- 分页模糊查询(链接)
- Java--四种权限(private,默认的,protected,public)
- java线程安全总结
- Wrong FS: hdfs://localhost:9000/user/... expected: file:///
- 题目1145:Candy Sharing Game
- java.lang.RuntimeException: Method called after release()
- C#设计模式01:单件模式(Singleton Pattern)
- document.write的问题
- python学习笔记之二:字符串操作
- 智能制造核心技术在哪里?
- 17、spring AOP通知——Pointcut、Advisor
- 多线程用到的概念知识点
- Android WIFI IP