# 分糖果 **问题描述** 有n个小朋友围坐成一圈。老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏: 每个小朋友都把自己的糖果分一半给左手边的孩子。 一轮分糖后,拥有奇数颗糖的孩子由老师补给1个糖果,从而变成偶数。 反复进行这个游戏,直到所有小朋友的糖果数都相同为止。 你的任务是预测在已知的初始糖果情形下,老师一共需要补发多少个糖果。 **输入格式** 程序首先读入一个整数N(2错误的一项是? ## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp ``` ## 答案 ```cpp int main() { int n; cin >> n; int a[n], tmp[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int cnt = 0, t; bool f = true; while (true) { f = true; for (int i = 1; i < n; ++i) if (a[0] != a[i]) { f = false; break; } if (f) break; for (int i = 0; i < n; ++i) a[i] = tmp[i] = a[i] >> 1; for (int i = 0; i < n; ++i) { t = (i - 1 + n) % n; a[t] += tmp[i]; if (a[t] && 1) ++a[t], ++cnt; } } cout << cnt; return 0; } ``` ## 选项 ### A ```cpp int main() { int n, m, count = 0, t = 0; cin >> n; vector v; for (int i = 0; i < n; i++) { cin >> m; v.push_back(m); } while (1) { bool flag = true; for (int i = 0; i < n; i++) { if (v[0] != v[i]) flag = false; } if (flag) break; for (int i = 0; i < n; i++) { v[i] = v[i] / 2; } int temp = v[n - 1]; for (int i = n - 1; i > 0; i--) { v[i] += v[i - 1]; } v[0] += temp; for (int i = 0; i < n; i++) { if (v[i] & 1) { v[i]++; count++; } } } cout << count << endl; return 0; } ``` ### B ```cpp int n; int a[100]; int main() { bool flag; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int nAns = 0; int temp; while (true) { for (int i = 1; i <= n; i++) { if (1 == i) { temp = a[i] / 2; a[i] -= temp; } else { a[i - 1] += a[i] / 2; a[i] -= a[i] / 2; } } a[n] += temp; for (int i = 1; i <= n; i++) { if (a[i] % 2 == 1) { a[i] += 1; nAns++; } } flag = true; for (int i = 1; i < n; i++) { if (a[i] != a[i + 1]) { flag = false; } } if (flag == true) { break; } } cout << nAns << endl; return 0; } ``` ### C ```cpp int main() { int n; cin >> n; int *Candy = new int[n]; for (int i = 0; i < n; i++) { cin >> Candy[i]; } int total = 0; while (1) { bool flag = true; for (int i = 1; i < n; i++) { if (Candy[0] != Candy[i]) { flag = false; break; } } if (flag == true) break; int temp = Candy[n - 1]; for (int i = n - 1; i >= 0; i--) { if (i == 0) { Candy[0] = Candy[0] / 2 + temp / 2; } else Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2; if (Candy[i] % 2 != 0) { Candy[i] += 1; total += 1; } } } cout << total << endl; } ```