1028: 糖果游戏——Power OJ

Description
糖果游戏,有N(n<1000)个学生坐成一圈,每个学生最初都有偶数个糖果,当老师下口令的时候,每个学生同时都把手中糖果的一半分给右边的学生,如果某个学生手中的糖果数是奇数,那么老师就再给他一块糖果,继续进行游戏。当每个学生手中的糖果数相同的时候,游戏结束。
Input
每组testcase输入一个N表示学生数,然后N行代表每个学生最初手中的糖果数。当N为0时,输入结束。
Output
对于每组testcase输出一行,每行输出两个数,以空格隔开,分别代表游戏进行的次数和最终学生手中的糖果数。

Notes:
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
    Sample Input
    Raw
    6
    36
    2
    2
    2
    2
    2
    11
    22
    20
    18
    16
    14
    12
    10
    8
    6
    4
    2
    4
    2
    4
    6
    8
    0
    Sample Output
    Raw

    15 14
    17 22
    4 8

解题思路:简单模拟一下

#include<stdio.h>
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)  {
        int a[n];
        int flag = 1,k = 0,temp = 0;
        for(int i = 0;i < n;i++) {
            scanf("%d",&a[i]);
        }
        while(flag)
        {
            flag = 0;
            for(int i = 1;i < n;i++)   //判断是否全部相等,如果不相等,flag置为1继续循环。
            {
                if(a[i-1] != a[i])
                {
                    flag = 1;
                }
            }
            temp = a[n-1]/2;    //不相等就排序。
            a[n-1] = a[n-1]/2;
            for(int i = n-1;i > 0;i--)
            {
                a[i] += a[i-1]/2;
                a[i-1] = a[i-1]/2;
            }
            a[0] = a[0] + temp;
            for(int i=0;i<n;i++)
                if(a[i]%2 != 0)
                    a[i]++;
            k++;
        }
        printf("%d %d\n",k-1,a[0]);

    }
  return 0;
}

发布了36 篇原创文章 · 获赞 10 · 访问量 1920

猜你喜欢

转载自blog.csdn.net/weixin_44003265/article/details/100586595
OJ