Solution for a candy game

Source: Internet
Author: User
All the students circle around the teacher. Each student starts with an even number of candy blocks. Each time the teacher blows a whistle, each student gives half of the candy in his hands to the students on the right, after the whistle, when the number of candy in the hand is odd, the teacher will fill it with him or her. In this way, when the number of candy in each student's hand is the same, the game ends.
Input requirement: there are more than one game test. For each game test, enter the number of students n, and the number of sweets (even) in each student's hand at the beginning ), the test ends when the number of students is 0.
Output requirements: For each game test, the number of sweets in the hands of the students is the same after the instructor blew several whistle, and the number of candy in the same row is the same.
Standard Input: 6
36
2
2
2
2
2
4
2
4
6
8
Standard output: 15 14
4 8

The output of the solution is different from the instance output, mainly because the number of whistle-blowing times varies by the Teacher:

# Include <iostream>
Using namespace STD;

Void Arun (int * a, int nlen, int * n, int * How)
{
Int J, T1, T2;
Int I = 0;
While (1)
{
I ++;
T2 = A [nlen-1]/2;
For (j = 0; j <nlen; j ++)
{
T1 = A [J]/2;
A [J] = t1 + T2;
T2 = T1;
}
For (j = 1; j <nlen; j ++)
{
If (A [0]! = A [J])
Break;
}
If (j = nlen ){
* N = I;
* How = A [0];
Return;
}
For (j = 0; j <nlen; j ++)
{
If (A [J] % 2 = 1) A [J] ++;
}
}
}

Int main ()
{
Int N, how;
While (1)
{
If (CIN> N)
{
If (n <= 0) break;
Int * A = (int *) New int [N];
For (INT I = 0; I <n; I ++)
{
Cin> A [I];
}
Arun (A, N, & N, & how );
Delete [];
Cout <n <"" }
Else break;
}
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.