hduoj 1034
来源:互联网 发布:github 网络中立 编辑:程序博客网 时间:2024/06/06 19:44
Candy Sharing Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5890 Accepted Submission(s): 3580
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
6362222211222018161412108642424680
Sample Output
15 1417 224 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.比较坑的地方是一个人必须先给下一个才能拿上一个人的,之前一直理解错误代码:比比较#include<algorithm>#include<iostream>#include<cstring> #include<cmath>using namespace std;int cake[10010];int n;int pan(){ int t = cake[0]; for(int i = 1;i < n;i++) { if(t != cake[i]) { return 1; } } return 0;}int main() { while(cin >> n&&n) { for(int i = 0;i < n;i++) cin >> cake[i]; int ans = 0; while(pan()) { ans++; int tem = cake[n-1]/2; for(int i = n-1;i > 0;i--) { cake[i] = cake[i]/2+cake[i-1]/2; if(cake[i] % 2) cake[i]++; } cake[0] = cake[0]/2 + tem; if(cake[0] % 2) cake[0]++; } cout << ans << " " << cake[0] << endl; } return 0;}
0 0
- hduoj 1034
- HDUOJ
- HDUoj
- HDUOJ 2050
- HDUOJ 2018
- HDUOJ 2069
- HDUOJ 1303
- HDUOJ 2031
- HDUOJ 2047
- HDUOJ 2089
- hduoj 1708
- hduoj 2031
- hduoj 2032
- hduoj 1062
- HDUOJ 1039
- HDUOJ lines
- HDUOJ入门
- HDUOJ 水果
- 2016年提高组模拟题(20161114) 锦标赛
- 数据结构 排序(冒泡排序改进,简单选择排序链表实现)
- MySQL study之--事务隔离级别
- Android UI PopupWindow 简单使用
- 【NOIP2016提高A组模拟9.14】数列编辑器
- hduoj 1034
- 魔术师纸牌,有空再研究
- win10 + VS2013下载directshow相关示例代码及directshow库文件调用
- 2016年提高组模拟题(20161114) 物品选取
- 每日一题(4),poj1789, Truck History
- 洛谷 P1595 信封问题
- mysql之删除用户,和修改权限
- 进程/线程同步——Critical Section,Mutex,Semaphore,Event区别
- AVAudioPlayer播放本地音乐