acm-simulation of candy sharing game--hdu1034

Source: Internet
Author: User
Candy sharing Game Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)

Total Submission (s): 2897 Accepted Submission (s): 1811


Problem Description A 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.
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 of the 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, bot h on one line.
Sample Input

6-2 2 2 2 2-ten-8 6 4 2 4 2 4 6 8 0 The Sample Output
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 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 C   Ount increase. Source Greater New York 2003 title: http://acm.hdu.edu.cn/showproblem.php?pid=1034

HDU on a simulated problem, quite water. Some students sit around the teacher in a circle, and everyone has some sugar in their hands. As soon as the teacher blew the whistle, the students divided the sugar to the right-hand side of the class. If a student has an odd number of sugars in his hand, the teacher gives him an extra sugar and an even number. Asking the teacher how many times to blow the whistle can make the number of sugars in each student's hand equal. The number of output whistle and sugar.
is to simulate the situation of sugar, if the odd number is +1. Notice the decision at the boundary. I was more open an array of space, and then, each time behind the get the front of the person sugar. The leftmost space is initialized to the rightmost space. This step-by-step simulation comes out. To determine parity or bitwise operations,%2 is 15ms,&1 0MS.
/************************************** *************************************** * Author:tree * *Fr   Om:http://blog.csdn.net/lttree * * Title:candy sharing Game * *SOURCE:HDU 1034 * Hint : Simulated question * *************************************** **************************************/#include &L
T;iostream> using namespace std;
int arr[100001];
    BOOL judge (int n) {int i;
    for (I=2;i<=n;++i) if (Arr[1]!=arr[i]) return false;
return true;
    } int main () {int i,test,step;
        while (cin>>test && test) {for (i=1;i<=test;++i) cin>>arr[i];
        Step=0;
            while (!judge (test)) {Arr[0]=arr[test];
                for (i=test;i>0;--i) {arr[i]= (ARR[I-1]/2+ARR[I]/2);
            Bit operation judge parity, certainly faster than%2 if (arr[i]&1) ++arr[i];
        } ++step;} cout<<step<< "" <<arr[1]<<endl;
} 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.