Sicily.1052. Candy Sharing Game

/*1052. Candy Sharing Game
  大意:同学坐成一圈,逆时针给自己手中一半糖果(同时!!!)
        一轮结束后,奇数个糖果的同学,老师给1个补充。
        求需要多少轮后,所有同学手中糖果数一样多,并且输出每人的糖果数 

*/
#include<iostream>
#include<stdlib.h>

using namespace std;

int N;
int candy[10000];
int half[10000];
int num;

int getRoundNum(){
    bool ok = true;
    while(1){
      num++;
      
      int start, passNum, tempStart, end;
      for(int i =0; i<N; i++){
         half[i] = candy[i]/2;
         candy[i] /= 2;
      }
      for(int i = 0; i<N; i++){
          candy[(i+1)%N] += half[i];
      }
      
      for(int i =0; i<N; i++){
         if(candy[i] % 2 == 1)
            candy[i]++;
      }
      ok = true;
      for(int i =1; i<N; i++){
         if(candy[i] != candy[0]){
             ok = false;     
         }
      }
      if(ok) {
         return num;
      }
    }
    return num;
}

int main()
{
 
    while(cin >> N && N!=0){
        num = 0;
        for(int i = 0; i<N; i++)
          cin >> candy[i];
       int n = getRoundNum();   
       //这里注意,不能写成  cout << getRoundNum() << " " << candy[0] << endl; 
       //不知道为啥,可能是因为cout时先把后面的入栈,那么candy[0]在调用getRoundNum之前,出错了。。 
       cout << n << " " << candy[0] << endl; 
                  
    }
    system("pause");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值