HDU 1034 candy sharing game (Water Simulation)

Source: Internet
Author: User
Candy sharing game

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1685 accepted submission (s): 1045

Problem descriptiona 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.

 

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 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 input6 36 2 2 2 2 2 11 22 20 16 14 12 10 8 6 4 2 2 4 6 8 0

 

Sample output15 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.

 

Sourcegreater New York 2003 .... Very simple simulation .... Pay attention to first give it to others, and then get it from others. The main boundary is handled.
# Include <stdio. h> Const   Int Maxn = 1000  ;  Int  A [maxn];  Int Main (){  Int  N;  Int  I;  While (Scanf ( "  % D  " ,& N), n ){  For (I = 0 ; I <n; I ++) scanf ( "  % D  " ,&A [I]);  Int Res = 0  ;  While ( 1  ){  For (I = 1 ; I <n; I ++ )  If (A [I- 1 ]! = A [I]) Break  ;  If (I> = N) Break  ; Res ++ ;  Int Temp = A [n- 1 ]/ 2  ;  For (I = N- 1 ; I> 0 ; I -- ) {A [I] /= 2  ; A [I] + = A [I-1 ]/ 2  ;} [  0 ]/= 2  ; [  0 ] + = Temp;  For (I = 0 ; I <n; I ++ )  If (A [I] & 1 ) A [I] ++ ;} Printf ( "  % D \ n  " , Res, [ 0  ]);}  Return   0  ;} 

 

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.