"HDU" 1034 Candy sharing Game

Source: Internet
Author: User
Candy sharing Game Topic linksCandy sharing Game Main Topic

That is, there are n small students, each classmate has a certain number of candies, now press counter-clockwise to stand a ring, every time the teacher whistled, each child will put the candy in the hand half to the right of the children, if he is in his hand is odd number of sweets, he will get a candy from the teacher, ask now need to blow a few rounds And finally the number of sweets in each child's hand. Solving

Originally thought can launch a magic mathematical formula, the result simulation is over ... Code

 #include <iostream> #include <cstdio> #include <cstring> using namespace

Std

int a[2][1000005],p,cnt,ans,n;
    BOOL Check (int k) {for (int i=1;i<n;i++) if (a[k][i]!=a[k][0]) return 0;
return 1;
        } void Solve (int k) {for (int i=1;i<=n;i++) {a[k%2][i%n]=a[(k+1)%2][i%n]/2+a[(k+1)%2][(i-1)%N]/2;
            if (a[k%2][i%n]%2) {a[k%2][i%n]++;
        ans++;
        }}} int main () {while (scanf ("%d", &n), n!=0) {memset (a,0,sizeof (a)); cnt=0;
        P=0;
        for (int i=0;i<n;i++) scanf ("%d", &a[0][i]);
            while (!check (p%2)) {solve (++p);
        cnt++;
    } printf ("%d%d\n", cnt,a[p%2][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.