成绩层次 利用switch语句判断小明的成绩在班里的层次(满分100):>=90分,优秀;>=80分,良;>=60分,及格;<60分,不及格。
– 输入描述: 一行,一个整数表示小明的成绩 输出描述: 一行,一个字符串,优秀为"perfect",良为"great",及格为"good",不及格为"miss"
int main() {
int s, flag;
cin >> s;
if (s>=90) {
flag = 1;
}
else if (s>=80) {
flag = 2;
}
else if (s>=60) {
flag = 3;
}
else {
flag = 4;
}
switch (flag) {
case 1 : cout << "perfect"; break;
case 2 : cout << "great"; break;
case 3 : cout << "good"; break;
case 4: cout << "miss"; break;
}
return 0;
}
答案的:
int main() {
int a;
cin >> a;
switch ((a >= 90) + (a >= 80) + (a >= 60)) {
case 3: cout << "perfect"; break;
case 2: cout << "great"; break;
case 1: cout << "good"; break;
case 0: cout << "miss"; break;
}
return 0;
}
|