杭电 1034:Candy Sharing Game

来源:互联网 发布:图像对比度算法 编辑:程序博客网 时间:2024/05/16 17:21

Problem Description

A 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 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.
每次游戏,输入由N个学生开始,其次由N个糖果数(偶数)为每个孩子逆时针绕一圈。
The input ends with a student count of 0.
输入以学生数为0结束。
Each 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, both on one line.
每一次游戏,输出游戏的圈数接着是每个孩子结束时的糖果数,请按都在一条线上。


CODE
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char *argv[],int n)  
  4. {  
  5.     while(~scanf("%d",&n) && n)  
  6.     {  
  7.         int a[n],i,flag = 1,k = 0,temp = 0;  
  8.         for(i = 0;i < n;i++)  
  9.         {  
  10.             scanf("%d",&a[i]);  
  11.         }  
  12.         while(flag)  
  13.         {  
  14.             flag = 0;  
  15.             for(i = 1;i < n;i++)   //判断是否全部相等,如果不相等,flag置为1继续循环。  
  16.             {  
  17.                 if(a[i-1] != a[i])  
  18.                 {  
  19.                     flag = 1;  
  20.                 }  
  21.             }  
  22.             temp = a[n-1]/2;    //不相等就排序。  
  23.             a[n-1] = a[n-1]/2;  
  24.             for(i = n-1;i > 0;i--)  
  25.             {  
  26.                 a[i] += a[i-1]/2;  
  27.                 a[i-1] = a[i-1]/2;  
  28.             }  
  29.             a[0] = a[0] + temp;  
  30.             for(i=0;i<n;i++)  
  31.                 if(a[i]%2 != 0)  
  32.                     a[i]++;  
  33.             k++;  
  34.         }  
  35.         printf("%d %d/n",k-1,a[0]);  
  36.           
  37.     }  
  38.   return 0;  


原创粉丝点击