1 条题解
-
0
【例31.2】 证书等级
解题思路
排名为比小蓝分高的人数加 1,同分不会排在他前面。将排名乘 100,依次与 10n、30n、60n、80n 比较确定等级。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
同分使用严格 > 统计更高分人数;每个奖项范围包含前一等级,须按顺序判断。
复杂度
时间 O(n),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int score, n; cin >> score >> n; int rank = 1; for (int i = 1; i < n; i++) { int x; cin >> x; if (x > score) rank++; } cout << (rank * 100 <= 10 * n ? "A" : rank * 100 <= 30 * n ? "B" : rank * 100 <= 60 * n ? "C" : rank * 100 <= 80 * n ? "D" : "E") << "\n"; return 0; }
- 1