1 条题解
-
0
【例34.1】 计分程序
解题思路
每组分别累计前10题每题6分、11~20题每题2分、21~40题每题1分,超过40题统一满分。读到文件结束处理多组。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
输入没有组数,不能把首个数字当 T;各段是累进计分。
复杂度
g 组时间 O(g),空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { int ans = min(n, 10) * 6 + min(max(n - 10, 0), 10) * 2 + min(max(n - 20, 0), 20); cout << ans << "\n"; } return 0; }
- 1