跳轉到

練習題目

Problem. 年齡分類

請輸入 int age,判斷:

  • 小於 0:輸出 年齡錯誤
  • 0 到 12:輸出 兒童
  • 13 到 17:輸出 青少年
  • 18 到 64:輸出 成人
  • 65 以上:輸出 長者
#include <iostream>
using namespace std;

int main() {
    int age = 0;

    cout << "請輸入年齡:";
    cin >> age;

    if (age < 0) {
        cout << "年齡錯誤" << endl;
    } else if (age <= 12) {
        cout << "兒童" << endl;
    } else if (age <= 17) {
        cout << "青少年" << endl;
    } else if (age <= 64) {
        cout << "成人" << endl;
    } else {
        cout << "長者" << endl;
    }

    return 0;
}

Problem. 密碼長度檢查

請輸入 string password,判斷密碼長度:

  • 小於 8:輸出 密碼太短
  • 8 到 16:輸出 密碼長度合格
  • 超過 16:輸出 密碼太長

提示:使用 password.length()

#include <iostream>
#include <string>
using namespace std;

int main() {
    string password;

    cout << "請輸入密碼:";
    cin >> password;

    if (password.length() < 8) {
        cout << "密碼太短" << endl;
    } else if (password.length() <= 16) {
        cout << "密碼長度合格" << endl;
    } else {
        cout << "密碼太長" << endl;
    }

    return 0;
}

Problem. BMI

請輸入 double heightCmdouble weightKg,計算 BMI:

BMI = 體重 / (身高公尺 * 身高公尺)

分類:

  • BMI < 18.5:過輕
  • BMI < 24:正常
  • BMI < 27:過重
  • 其他:肥胖

提示:公分要除以 100 變公尺。

#include <iostream>
using namespace std;

int main() {
    double heightCm = 0.0;
    double weightKg = 0.0;

    cout << "請輸入身高(cm):";
    cin >> heightCm;

    cout << "請輸入體重(kg):";
    cin >> weightKg;

    double heightM = heightCm / 100.0;
    double bmi = weightKg / (heightM * heightM);

    cout << "BMI:" << bmi << endl;

    if (bmi < 18.5) {
        cout << "過輕" << endl;
    } else if (bmi < 24) {
        cout << "正常" << endl;
    } else if (bmi < 27) {
        cout << "過重" << endl;
    } else {
        cout << "肥胖" << endl;
    }

    return 0;
}

Problem. 閏年判斷

請輸入 int year,判斷是否閏年。

規則:

  • 能被 400 整除:閏年
  • 能被 100 整除:不是閏年
  • 能被 4 整除:閏年
  • 其他:不是閏年
#include <iostream>
using namespace std;

int main() {
    int year = 0;

    cout << "請輸入年份:";
    cin >> year;

    if (year % 400 == 0) {
        cout << "閏年" << endl;
    } else if (year % 100 == 0) {
        cout << "不是閏年" << endl;
    } else if (year % 4 == 0) {
        cout << "閏年" << endl;
    } else {
        cout << "不是閏年" << endl;
    }

    return 0;
}

Problem. 簡易計算機

請輸入 double a, bchar op

支援:

  • +
  • -
  • *
  • /

如果除以 0,輸出 不可除以 0

#include <iostream>
using namespace std;

int main() {
    double a = 0.0;
    double b = 0.0;
    char op = '+';

    cout << "請輸入算式,例如 10 + 3:";
    cin >> a >> op >> b;

    switch (op) {
        case '+':
            cout << "結果:" << a + b << endl;
            break;
        case '-':
            cout << "結果:" << a - b << endl;
            break;
        case '*':
            cout << "結果:" << a * b << endl;
            break;
        case '/':
            if (b == 0) {
                cout << "不可除以 0" << endl;
            } else {
                cout << "結果:" << a / b << endl;
            }
            break;
        default:
            cout << "不支援的運算子" << endl;
            break;
    }

    return 0;
}

Problem. 停車費計算

請輸入 int hours

規則:

  • 小於等於 0:輸出 時數錯誤
  • 第 1 小時 40 元
  • 第 2 小時起,每小時 30 元
  • 當日最高 200 元

提示:可以先算出費用,再判斷是否超過 200。

#include <iostream>
using namespace std;

int main() {
    int hours = 0;

    cout << "請輸入停車時數:";
    cin >> hours;

    if (hours <= 0) {
        cout << "時數錯誤" << endl;
        return 0;
    }

    int fee = 40;

    if (hours >= 2) {
        fee = fee + (hours - 1) * 30;
    }

    if (fee > 200) {
        fee = 200;
    }

    cout << "停車費:" << fee << " 元" << endl;
    return 0;
}