{ }codelper
三元运算符P0372

寄存柜的加收费用

火车站行李寄存柜的基本费用是 b 韩元。
行李寄存的时间不超过 h 小时,就没有加收费用(0 韩元)。
寄存的时间比 h 小时长,就要多付一次加收费用 e 韩元。
第一行给出 b、h、e,用一个空格隔开。
第二行给出顾客人数 n。
接下来的 n 行,每行给出一位顾客寄存行李的时间 t。
请按输入顺序,每行一个,输出每位顾客应付的费用(基本费用 + 加收费用)。

输入
1 ≤ b ≤ 10,000,1 ≤ h ≤ 100,1 ≤ e ≤ 10,000,1 ≤ n ≤ 100,1 ≤ t ≤ 200(均为整数)
输出
n 行,每行一位顾客的费用。
不写逗号和单位,只写数字
样例 1 · stdin
1000 3 500
3
2
3
5
stdout
1000
1000
1500
样例 2 · stdin
700 24 700
2
24
25
stdout
700
1400
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

三元运算符是在两个值中选一个的表达式,所以可以直接放进加法里。
C、C++、Java、C#、JS、TS、Craykon 的顺序是 条件 ? 为真时的值 : 为假时的值,写成 b + (t <= h ? 0 : e);只有 Python 的顺序是 为真时的值 if 条件 else 为假时的值,写成 b + (0 if t <= h else e)。
陷阱一:去掉括号,+ 会先结合。
在 C、C++、Java、C#、JS、TS、Craykon 中,b + t <= h ? 0 : e 被读成 (b + t <= h) ? 0 : e,费用不是 0 就是 e(例 1 变成 500 500 500)。
在 Python 中,b + 0 if t <= h else e 被读成 (b + 0) if t <= h else e,超时的顾客只付 e(例 1 变成 1000 1000 500)。
陷阱二:不用三元运算符,而用 JS (t <= h && 0) || e 或 Python t <= h and 0 or e 来模仿,前面的值 0 会被当作假,条件为真时也得到 e(例 1 变成 1500 1500 1500)。
陷阱三:“不超过 h 小时”是 <=。
写成 <,正好寄存 h 小时的顾客也要付加收费用(例 1 变成 1000 1500 1500)。

Pythonmain.py · 7 行
b, h, e = map(int, input().split())n = int(input())for _ in range(n):    t = int(input())    # 값 if 조건 else 값 — 덧셈 안에 넣을 때는 괄호로 묶는다    fee = b + (0 if t <= h else e)    print(fee)
JavaScriptmain.js · 10 行
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [b, h, e, n] = nums;const out = [];for (let i = 0; i < n; i++) {  const t = nums[4 + i];  // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다  const fee = b + (t <= h ? 0 : e);  out.push(fee);}console.log(out.join("\n"));
TypeScriptmain.ts · 11 行
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [b, h, e, n] = nums;const out: number[] = [];for (let i = 0; i < n; i++) {  const t: number = nums[4 + i];  // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다  const fee: number = b + (t <= h ? 0 : e);  out.push(fee);}console.log(out.join("\n"));
JavaMain.java · 15 行
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int b = sc.nextInt(), h = sc.nextInt(), e = sc.nextInt();        int n = sc.nextInt();        for (int i = 0; i < n; i++) {            int t = sc.nextInt();            // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다            int fee = b + (t <= h ? 0 : e);            System.out.println(fee);        }    }}
C#Main.cs · 18 行
using System; class Program{    static void Main()    {        string[] head = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int b = int.Parse(head[0]), h = int.Parse(head[1]), e = int.Parse(head[2]);        int n = int.Parse(Console.ReadLine());        for (int i = 0; i < n; i++)        {            int t = int.Parse(Console.ReadLine());            // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다            int fee = b + (t <= h ? 0 : e);            Console.WriteLine(fee);        }    }}
C++main.cpp · 13 行
#include <iostream> int main() {    int b, h, e, n;    std::cin >> b >> h >> e >> n;    for (int i = 0; i < n; i++) {        int t;        std::cin >> t;        // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다        int fee = b + (t <= h ? 0 : e);        std::cout << fee << "\n";    }}
Cmain.c · 14 行
#include <stdio.h> int main(void) {    int b, h, e, n;    if (scanf("%d %d %d %d", &b, &h, &e, &n) != 4) return 0;    for (int i = 0; i < n; i++) {        int t;        if (scanf("%d", &t) != 1) return 0;        /* 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다 */        int fee = b + (t <= h ? 0 : e);        printf("%d\n", fee);    }    return 0;}
Craykonmain.cray · 12 行
head = 控制台.读数字();b = head[0];h = head[1];e = head[2];n = 控制台.读行().转数字();循环 (i = 0; i < n; i++){    t = 控制台.读行().转数字();    // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다    fee = b + (t <= h ? 0 : e);    控制台.输出(fee);}