题解 | #Candy Sharing Game#
Candy Sharing Game
https://www.nowcoder.com/practice/33adf53e095440b2823a39146a285b77
//每次循环交换时,若手中糖果为奇数,则多的那一个留给自己。题比较简单,但是题意没有讲清楚 #include "stdio.h" #include "queue" using namespace std; int student[1000];//记录学生的糖果数,从student[1]开始 bool equalJudge(int N){//满足条件返回true int candy = student[1]; for (int i = 1; i <= N; ++i) { if (student[i] != candy){ return false; } } return true; } void Exchange(int N,int &count){ int shadow[1000];//studengt[]的影子数组 for (int i = 1; i <= N; ++i) { shadow[i] = student[i]; } for (int i = 1; i <= N; ++i) {//进行一轮循环 if (student[i]%2 == 1){ if (i != 1) student[i] = student[i]/2 + shadow[i-1]/2 + 1; else student[i] = student[i]/2 + shadow[N]/2 + 1; } else{ if (i != 1) student[i] = student[i]/2 + shadow[i-1]/2; else student[i] = student[i]/2 + shadow[N]/2; } } for (int i = 1; i <= N; ++i) { if (student[i]%2 == 1) ++student[i]; } ++count; } int main(){ int N;int count;//count记录响哨次数 while (scanf("%d",&N)!=EOF){ if (N == 0) return 0; count = 0; for (int i = 1; i <= N; ++i) { scanf("%d",student+i); } while (equalJudge(N)!=true){ Exchange(N,count); } printf("%d %d\n",count,student[1]); } }