{ }codelper
if / elsenested conditionsP0191

Member discount and delivery fee

A neighborhood bakery sets the payment for each online order. The first line gives the number of orders n, and each of the next n lines gives the customer type and the order amount, separated by one space. The customer type is M for a member and G for a guest. A member gets 2,000 won off if the order amount is 20,000 won or more, and pays the order amount as it is if it is less than 20,000 won. A guest pays the order amount plus a delivery fee of 3,000 won, whatever the amount. Print the payment of each order on its own line, in input order. On the last line print the sum of the payments.

Input
1 ≤ n ≤ 1,000, 1,000 ≤ order amount ≤ 1,000,000 (integer), customer type is M or G
Output
n lines of payments, then their sum (plain integers, without commas or "won")
Example 1 · stdin
4
M 25000
M 18000
G 18000
M 20000
stdout
23000
18000
21000
18000
80000
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

With an if inside an if, the trap is which if an else belongs to.
In C, C++, Java, C#, JS and TS, without braces an else attaches to the nearest inner if (the dangling else).
So if (member) if (amount >= 20000) discount; else fee;, indented as if the else were outer, charges the fee to members under 20,000 won and lets guests pass without it.
Craykon also allows bodies without braces, and its else attaches to the nearest if too.
Braces on every outer if prevent this.
In Python indentation is the syntax, so an else: in the same column as the outer if belongs to the outer one.
Another common mistake is merging the two conditions into member && amount >= 20000.
Then the else covers not only guests but also members under 20,000 won.
Exactly 20,000 won is the boundary and needs >= to get the discount.
The sum is at most 1,003,000,000 won, which fits a 32-bit integer.

Pythonmain.py · 13 lines
n = int(input())total = 0for _ in range(n):    kind, amount = input().split()    pay = int(amount)    if kind == "M":        if pay >= 20000:            pay -= 2000    else:        pay += 3000  # 비회원은 금액과 상관없이 배달비    print(pay)    total += payprint(total)
JavaScriptmain.js · 19 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];let total = 0;for (let i = 1; i <= n; i++) {  const [kind, amount] = lines[i].trim().split(" ");  let pay = Number(amount);  if (kind === "M") {    if (pay >= 20000) {      pay -= 2000;    }  } else {    pay += 3000; // 비회원은 금액과 상관없이 배달비  }  out.push(pay);  total += pay;}out.push(total);console.log(out.join("\n"));
TypeScriptmain.ts · 20 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out: number[] = [];let total = 0;for (let i = 1; i <= n; i++) {  const [kind, amount] = lines[i].trim().split(" ");  let pay = Number(amount);  if (kind === "M") {    if (pay >= 20000) {      pay -= 2000;    }  } else {    pay += 3000; // 비회원은 금액과 상관없이 배달비  }  out.push(pay);  total += pay;}out.push(total);console.log(out.join("\n"));
JavaMain.java · 26 lines
import java.io.BufferedReader;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(in.readLine().trim());        StringBuilder out = new StringBuilder();        long total = 0;        for (int i = 0; i < n; i++) {            String[] p = in.readLine().trim().split(" ");            int pay = Integer.parseInt(p[1]);            if (p[0].equals("M")) {                if (pay >= 20000) {                    pay -= 2000;                }            } else {                pay += 3000; // 비회원은 금액과 상관없이 배달비            }            out.append(pay).append('\n');            total += pay;        }        out.append(total).append('\n');        System.out.print(out);    }}
C#Main.cs · 32 lines
using System;using System.Text; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        var sb = new StringBuilder();        long total = 0;        for (int i = 0; i < n; i++)        {            string[] p = Console.ReadLine().Trim().Split(' ');            int pay = int.Parse(p[1]);            if (p[0] == "M")            {                if (pay >= 20000)                {                    pay -= 2000;                }            }            else            {                pay += 3000; // 비회원은 금액과 상관없이 배달비            }            sb.Append(pay).Append('\n');            total += pay;        }        sb.Append(total).Append('\n');        Console.Write(sb);    }}
C++main.cpp · 23 lines
#include <iostream>#include <string> int main() {    int n;    std::cin >> n;    long long total = 0;    for (int i = 0; i < n; i++) {        std::string kind;        int pay;        std::cin >> kind >> pay;        if (kind == "M") {            if (pay >= 20000) {                pay -= 2000;            }        } else {            pay += 3000; // 비회원은 금액과 상관없이 배달비        }        std::cout << pay << '\n';        total += pay;    }    std::cout << total << '\n';}
Cmain.c · 23 lines
#include <stdio.h> int main(void) {    int n;    scanf("%d", &n);    long long total = 0;    for (int i = 0; i < n; i++) {        char kind[2];        int pay;        scanf("%1s %d", kind, &pay);        if (kind[0] == 'M') {            if (pay >= 20000) {                pay -= 2000;            }        } else {            pay += 3000; /* 비회원은 금액과 상관없이 배달비 */        }        printf("%d\n", pay);        total += pay;    }    printf("%lld\n", total);    return 0;}
Craykonmain.cray · 21 lines
n = Console.ReadLine().ToNumber();total = 0;for (i = 0; i < n; i++){    p = Console.ReadLine().Trim().Split();    pay = p[1].ToNumber();    if (p[0] == "M")    {        if (pay >= 20000)        {            pay -= 2000;        }    }    else    {        pay += 3000; // 비회원은 금액과 상관없이 배달비    }    Console.WriteLine(pay);    total += pay;}Console.WriteLine(total);