码迷,mamicode.com
首页 > 其他好文 > 详细

杭电acm 1034题

时间:2014-05-07 17:53:27      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

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
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
 

 

Sample Output
15 14 17 22 4 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个开始都拿着偶数颗糖的小朋友围着老师坐,老师一吹哨子同时给自己右手边的同学自己一半的糖,结束后奇数颗糖的同学老师会给一颗糖补成偶数棵,直到大家手上的糖一样数目...问需要玩几次,以及最后糖的数量是多少?

bubuko.com,布布扣
 1 #include <iostream>
 2 using namespace std;
 3 #define Max 10000
 4 int judge(int *stu,int len);
 5 int main(void)
 6 {
 7     int student[Max];
 8     int N;
 9     int j=0;
10     int i=0;
11     int temp[Max];
12     int around=0;
13     cin>>N;
14     while(N)
15     {
16         around=0;
17         for(i=0;i<N;i++)
18             cin>>student[i];
19         for(j=0;;j++)
20         {
21             
22             //所有的都看看有没有奇数
23             for(i=0;i<N;i++)
24             {
25                 student[i]=student[i]/2;
26                 temp[i]=student[i];
27             }
28             for(i=1;i<N;i++)
29             {
30                 student[i]+=temp[i-1];
31             }
32             student[0]+=temp[N-1];
33             for(i=0;i<N;i++)
34             {
35                 if(student[i]%2==1)
36                     student[i]+=1;
37             }
38             around+=1;
39             if(judge(student,N))
40                 break;
41             
42         }
43         
44         cout<<around<<" "<<student[0]<<endl;
45         cin>>N;
46     }
47     return 0;
48 }
49 int judge(int *stu,int len)
50 {
51     int flag=1;
52     for(int i=1;i<len;i++)
53     {
54         if(stu[i-1]==stu[i])
55             continue;
56         else {flag=0;break;} 
57 
58     }
59     return flag;
60 
61 }
bubuko.com,布布扣

 

杭电acm 1034题,布布扣,bubuko.com

杭电acm 1034题

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/kb342/p/3710089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!