hdu1034(模拟题)
来源:互联网 发布:手机设计图纸软件 编辑:程序博客网 时间:2024/06/15 19:31
http://acm.hdu.edu.cn/showproblem.php?pid=1034
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.
n个学生围成一圈,每个人手上有a[i]个糖果,一个给下一个自己的一半,当所有人的糖果数一样时游戏结束。
输出总共进行了多少轮游戏 还有最后学生手中的糖果数。
ps:注意是一个学生得先给出糖果才能接受上一个学生给的,博主一开始没理解,结果一直wrong answeer 后来想到了4 2 2 2 这种情况发现是死循环,才回头去理解题意,几经波折才AC,学好语文很重要啊!贴上代码
#include<bits/stdc++.h>using namespace std;int a[1000];int n;bool check(){for(int i=1;i<n;i++){if(a[i]!=a[i-1]) return true;}return false;}int main(){int i,j,cnt=0;int t;while(scanf("%d",&n),n){cnt=0;for(i=0;i<n;i++) scanf("%d",&a[i]);while(check()){int y=a[n-1]/2; for(i=n-1;i>0;i--) { a[i]=a[i]/2+a[i-1]/2; if(a[i]%2) a[i]++; } a[0]=a[0]/2+y; if(a[0]%2) a[0]++;cnt++;}cout<<cnt<<" "<<a[0]<<endl;}}
1 0
- hdu1034(模拟题)
- hdu1034(简单模拟)
- HDU1034 - Candy Sharing Game (简单模拟)
- hdu1034 简单模拟
- 模拟:HDU1034-Candy Sharing Game
- hdu1034
- 2016 小灶练习1 A题 (HDU1034)
- ACM-模拟之Candy Sharing Game——hdu1034
- HDU1034 Candy Sharing Game(java)
- hdu1034-Candy Sharing Game
- hdu1034 Candy Sharing Game
- HDU1034 Candy Sharing Game
- HDU1034 Candy Sharing Game
- 水题:HDU1034-Candy Sharing Game
- 【NOIP 模拟题】[T1]return(模拟)
- hdu1013(模拟题)
- Miscalculation(模拟题)
- (模拟题)
- M.2扫盲篇之M.2连接器
- leecode 解题总结:63. Unique Paths II
- Spring MVC 使用 静态资源
- request.getRequestDispatcher().forward(request,response)和response.sendRedirect()的区别
- asp.net core mvc权限控制:在视图中控制操作权限
- hdu1034(模拟题)
- 1041. Be Unique 解析
- 宝钗
- 2017-2-10-w3cschool编程挑战 -javascript
- 快速排序的实现
- hibernate中两表一对一关系 和 session.save()方法和 session.saveOrUpdate()方法
- 1042. Shuffling Machine 解析
- PAT 1045.快速排序
- sessionStorage 、localStorage 和 cookie 对比区分