首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

HDU 成績轉換:百分制轉等級制(含異常輸入處理)

     編輯:關於C語言

題目規則

分數區間等級
90 ~ 100A
80 ~ 89B
70 ~ 79C
60 ~ 69D
0 ~ 59E

輸入超出 [0,100] 輸出 Score is error!。

解題邏輯

  1. 循環讀入整數直到 EOF
  2. 先判斷合法性 if (score < 0 || score > 100)
  3. 合法則按降序判斷區間(避免重複條件)

代碼實現

#include <stdio.h>

int main() {
    int score;
    while (scanf("%d", &score) != EOF) {
        if (score < 0 || score > 100) {
            printf("Score is error!\n");
        } else if (score >= 90) {
            printf("A\n");
        } else if (score >= 80) {
            printf("B\n");
        } else if (score >= 70) {
            printf("C\n");
        } else if (score >= 60) {
            printf("D\n");
        } else {
            printf("E\n");
        }
    }
    return 0;
}

關鍵點

  • 條件順序:先攔截非法,再從高到低匹配,利用 else if 互斥
  • 邊界包含:90~100 含兩端,score >= 90 正確覆蓋
  • 輸出格式:每行一結果,錯誤信息完全匹配大小寫與標點
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved