{ }codelper
ternary operatorP0372

Locker extra fee

A luggage locker at a train station has a base fee of b won. If the luggage is kept for h hours or less, there is no extra fee (0 won). If it is kept for longer than h hours, an extra fee of e won is added, just once. The first line gives b, h and e separated by single spaces. The second line gives the number of customers n. Each of the next n lines gives t, the number of hours one customer kept the luggage. For each customer, in input order, print the fee to pay (base fee + extra fee) on its own line.

Input
1 ≤ b ≤ 10,000, 1 ≤ h ≤ 100, 1 ≤ e ≤ 10,000, 1 ≤ n ≤ 100, 1 ≤ t ≤ 200 (all integers)
Output
n lines, one fee per customer.
Digits only, no commas or units
Example 1 · stdin
1000 3 500
3
2
3
5
stdout
1000
1000
1500
Example 2 · stdin
700 24 700
2
24
25
stdout
700
1400
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A ternary operator is an expression that picks one of two values, so it can sit right inside an addition.
C, C++, Java, C#, JS, TS and Craykon use the order condition ? value if true : value if false, giving b + (t <= h ? 0 : e); only Python uses value if true if condition else value if false, giving b + (0 if t <= h else e).
Trap one: without the parentheses, + binds first.
In C, C++, Java, C#, JS, TS and Craykon, b + t <= h ? 0 : e is read as (b + t <= h) ? 0 : e, so the fee becomes either 0 or e (example 1 becomes 500 500 500).
In Python, b + 0 if t <= h else e is read as (b + 0) if t <= h else e, so a customer over the time pays only e (example 1 becomes 1000 1000 500).
Trap two: imitating the ternary with JS (t <= h && 0) || e or Python t <= h and 0 or e fails because the first value, 0, counts as false, so e comes out even when the condition is true (example 1 becomes 1500 1500 1500).
Trap three: 'h hours or less' is <=.
With <, a customer who kept the luggage for exactly h hours also pays the extra fee (example 1 becomes 1000 1500 1500).

Pythonmain.py · 7 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
head = Console.ReadNumbers();b = head[0];h = head[1];e = head[2];n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    t = Console.ReadLine().ToNumber();    // 조건 ? 값 : 값 — 덧셈 안에 넣을 때는 괄호로 묶는다    fee = b + (t <= h ? 0 : e);    Console.WriteLine(fee);}