1 条题解
-
0
练13.4 糖果游戏
解题思路
顺序处理五位小朋友。当前糖果整除 3 得每份量,当前留一份,左右邻居各加一份,余数被吃掉。
正确性说明
每次操作只依据当前状态进行三分,并即时影响后续小朋友;依编号顺序模拟一轮符合过程。
易错点
不是五人同时分;首尾相邻;每数输出宽度为 5。
复杂度
本题输入规模固定,时间 O(1),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long a[5]; for (auto &x : a) cin >> x; for (int i = 0; i < 5; i++) { long long part = a[i] / 3; a[i] = part; a[(i + 4) % 5] += part; a[(i + 1) % 5] += part; } for (long long x : a) cout << setw(5) << x; cout << "\n"; return 0; }
- 1