Hdu-1034-candy sharing Game (c + + && simple Simulation)

Source: Internet
Author: User

Candy sharing GameTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3703 Accepted Submission (s): 2311


Problem Descriptiona number of students sit in a circle facing their teacher in the center. Each student initially have an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of he or her candy to the neighbor on the right. Any student, who ends-a odd number of pieces of candy, is given another piece by the teacher. The game ends when all students has the same number of pieces of candy.
Write a program which determines the number of times the 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.

Inputthe 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 of the input number is on a line by itself.

Outputfor 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
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 would ever decrease to the minimum.4. If the maximum and minimum candy count is not the same in least one student with the minimum amount must has their coun t increase.

Sourcegreater New York 2003
Recommendwe carefully selected several similar problems for you:1035 1037 1038 1018 1028

Referring to the method on the Internet, I have to say, is really incisive! Lean and easy to understand, learn!
First explain the topic:
A certain number of students in a circle, the teacher standing in the center of the circle. Each student starts with an even number of sweets. When the teacher blew a whistle, each student gave himself half the number of sweets to his right neighbour. All the students, after performing this treatment, if his current candy count is odd, then he can get a candy from the teacher's hand exceptionally. This game ends with the condition that all students have the same number of candies. Write a program to find out how many times the teacher whistled and the same number of candies each student had when the trip ended.
Wish roommate Cheng Lord, and the old Zhao Beijing Blue Bridge battle victory!! Next on the Daily one Water code (PS.) It seems that there is no guarantee to do it every day ...) :
//This is a simple simulation, pay attention to deal with boundary conditions on the line!    #include <iostream> #include <cstdio> #define N 100using namespace Std;int main () {int s[n],n,i,k,t,p;        while (scanf ("%d", &n) = = 1 && N) {for (i=0;i<n;i++) scanf ("%d", s+i); for (k=1;;                       k++) {T=S[N-1]/2;            First, t accesses the last person half of the candy number, that is, to deal with boundary conditions!                     for (i=0;i<n;i++) {P=S[I]/2;                P access the current half of the number of sweets used to give the right classmate S[i]=s[i]/2+t;                          Half of his own cut plus the left classmate to give half t=p;            Give a classmate of course to replace an if (s[i]%2!=0)//The current processing if an odd number, it will have to receive a teacher a s[i]++;            } T=s[0];     for (i=1;i<n&&s[0]==s[i];i++);            If each classmate has the same number of candies, the game is over!            if (i==n) {break;                }} printf ("%d%d\n", k,t); Output game count and each person final candy count} return 0;} 



Hdu-1034-candy sharing Game (c + + && simple Simulation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.