#44924: cpp easy answer


1121232@stu.wghs.tp.edu.tw (Ian911436)

學校 : 臺北市私立薇閣高級中學
編號 : 258883
來源 : [60.248.154.139]
最後登入時間 :
2025-02-05 12:49:24
d460. 山六九之旅 -- 板橋高中教學題 | From: [60.248.154.139] | 發表日期 : 2024-12-26 12:55

#include <iostream>

using namespace std;

int main() {
    // 輸入年齡
    int age;
    cin >> age;

    // 判斷票價
    int ticket_price = (age >= 0 && age <= 5) ? 0 :
        (age >= 6 && age <= 11) ? 590 :
        (age >= 12 && age <= 17) ? 790 :
        (age >= 18 && age <= 59) ? 890 :
        (age >= 60) ? 399 : -1;

    // 若票價為 -1,表示年齡無效
    if (ticket_price == -1) {
        cerr << "年齡輸入無效" << endl;
        return 1;
    }

    // 輸出票價
    cout << ticket_price << endl;

    return 0;
}

 
ZeroJudge Forum