【深基7.例11】评等级 · 教师题解
【深基7.例11】评等级
解题思路
定义学生结构体保存学号、两项成绩和综合分,并用成员函数返回两项总分。优秀要求总分 >140 且 7a+3b≥800。
正确性说明
综合分比较式两边乘 10,不改变不等号方向并避免浮点误差。两个条件同时成立恰好满足优秀定义。
易错点
总分条件是严格大于,综合分条件含等号;输出 Not excellent 的大小写与空格要一致。
复杂度
时间 O(N),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h>
using namespace std;
struct Student {
int id, a, b;
double score;
int sum() const {
return a + b;
}
};
bool excellent(const Student &s) {
return s.sum() > 140 && 7 * s.a + 3 * s.b >= 800;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
while (n--) {
Student s;
cin >> s.id >> s.a >> s.b;
s.score = (7 * s.a + 3 * s.b) / 10.0;
cout << (excellent(s) ? "Excellent" : "Not excellent") << "\n";
}
return 0;
}