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.
 

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 8
Hint
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.

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 哺乳期乳头破裂乳房似针扎怎么办 回奶胀痛的厉害怎么办 淡水龟的壳变软了怎么办 棕色的泰迪变白怎么办 大班教案泥石流来了怎么办 鸟类的嘴巴坏了怎么办 四川麦蚊子咬了怎么办 脸上被晒脱皮了怎么办 额头被晒脱皮了怎么办 脸黑一块白一块怎么办 小乌龟的壳软了怎么办 把田螺后面吃了怎么办 微生物生态菌群异常怎么办 怀孕初期感染了动物病毒怎么办? 金龙和银龙打架怎么办 海水缸盐度高了怎么办 洗空调洗坏了怎么办 老师是条青花鱼怎么办 吃了带鱼和南瓜怎么办 苹果平板ad忘了怎么办 小米6进海水了怎么办 小米手机掉海水里怎么办 苹果7进海水了怎么办 7p手机进海水怎么办 育海参苗出现红细菌怎么办 苹果手机掉进厕所怎么办 2个月的婴儿肺炎怎么办 甜甜圈珊瑚脱骨怎么办 宝宝吃了生物球怎么办 狗尾巴被剪掉了怎么办 魟鱼尾巴刺了怎么办 狗咬过了24小时怎么办 狗咬超过24小时怎么办 狗咬超过48小时怎么办 狗咬超过72小时怎么办 狗抓超过24小时怎么办 龙须树叶子发黄怎么办? 车被广告牌砸了怎么办 开花店压力好大怎么办 快成熟葡萄软果怎么办 木本叶长白斑点了怎么办