{ }codelper
nested conditionsfor-each loopP0136

Parking fee calculator

A parking lot charges each car by the minutes it stayed. First split the stay m into whole days of 1,440 minutes. Each full day costs 20,000 won. The fee for the remaining t minutes (m modulo 1,440) follows these rules. If t is 30 or less, it is 0 won. If t is more than 30, there is a base fee of 2,000 won, and this base fee covers the first 60 minutes. If t is more than 60, the time beyond 60 minutes is grouped into blocks of 10 minutes, and each block adds 500 won. A last block shorter than 10 minutes still counts as one block. If the fee for the remaining t minutes is more than 20,000 won, only 20,000 won is charged for them. The daily fees plus the fee for the remaining t minutes make the fee before discount. The kind of car is one of normal, compact and disabled. A disabled car gets 80% off and pays one fifth of the fee before discount. A compact car pays half of the fee before discount only when m is 1,440 or less; when m is more than 1,440 there is no discount. A normal car gets no discount. The first line gives the number of cars n, and each of the next n lines gives a car's kind and m separated by a single space. For each car, in input order, print its fee in won as an integer on its own line.

Input
1 ≤ n ≤ 1,000, 0 ≤ m ≤ 100,000, kind is one of normal, compact, disabled
Output
n lines, each a fee (integer)
Example 1 · stdin
5
normal 25
normal 95
compact 95
disabled 95
compact 1500
stdout
0
4000
2000
800
22000
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

The rules branch by kind first and then by time inside it, so nested conditions fit naturally.
If you merge the inner m <= 1440 check into kind == "compact" && m <= 1440, you must check that a compact car staying over a day does not fall into a later branch (here the last branch is 'no discount', so the answer is the same, but with more branches this goes wrong easily).
else-if is Python elif, C family, JS and TS else if, Craykon else if.
The most common trap is rounding the 10-minute blocks up.
Integer division truncates, so add 9 first: (t - 60 + 9) / 10 (Python //).
In JS, TS and Craykon / is real division, so write Math.ceil((t - 60) / 10) or Mathf.Ceil((t - 60) / 10).
Applying the 20,000 won cap once to the whole stay lets a car parked for several days pay only 20,000 won.
Take the full days off first and cap only the remaining minutes.
Looping over each item: Python for kind, m in cars (unpacking two values), Java for (String car : cars), C# foreach (string car in cars), C++ for (const auto& [kind, m] : cars), JS and TS for (const [kind, m] of cars) (of, not in), Craykon foreach (car in cars).
C has no for-each, so it loops by index.
String equality is Java equals, C strcmp(a, b) == 0, and == elsewhere (=== in JS and TS).

Pythonmain.py · 27 lines
import sys  def part_fee(t):    """하루가 안 되는 t분의 요금."""    if t <= 30:        return 0    fee = 2000    if t > 60:        fee += (t - 60 + 9) // 10 * 500  # 다 차지 않은 10분도 10분으로: 올림    return min(fee, 20000)  lines = sys.stdin.read().split("\n")n = int(lines[0])cars = [line.split() for line in lines[1:n + 1]]out = []for kind, minutes in cars:    m = int(minutes)    fee = m // 1440 * 20000 + part_fee(m % 1440)    if kind == "disabled":        fee = fee // 5  # 80% 할인 = 5분의 1    elif kind == "compact":        if m <= 1440:  # 하루를 넘기면 경차 할인이 없다            fee = fee // 2    out.append(str(fee))print("\n".join(out))
JavaScriptmain.js · 28 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n"); // 하루가 안 되는 t분의 요금function partFee(t) {  if (t <= 30) return 0;  let fee = 2000;  if (t > 60) {    fee += Math.ceil((t - 60) / 10) * 500; // 다 차지 않은 10분도 10분으로  }  return Math.min(fee, 20000);} const n = Number(lines[0]);const cars = lines.slice(1, n + 1).map((line) => line.trim().split(/\s+/));const out = [];for (const [kind, minutes] of cars) {  const m = Number(minutes);  let fee = Math.floor(m / 1440) * 20000 + partFee(m % 1440);  if (kind === "disabled") {    fee = fee / 5; // 80% 할인. fee는 500의 배수라 나누어떨어진다  } else if (kind === "compact") {    if (m <= 1440) {      fee = fee / 2; // 하루를 넘기면 경차 할인이 없다    }  }  out.push(fee);}console.log(out.join("\n"));
TypeScriptmain.ts · 29 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n"); // 하루가 안 되는 t분의 요금function partFee(t: number): number {  if (t <= 30) return 0;  let fee = 2000;  if (t > 60) {    fee += Math.ceil((t - 60) / 10) * 500; // 다 차지 않은 10분도 10분으로  }  return Math.min(fee, 20000);} const n: number = Number(lines[0]);const cars: string[][] = lines.slice(1, n + 1).map((line) => line.trim().split(/\s+/));const out: number[] = [];for (const [kind, minutes] of cars) {  const m: number = Number(minutes);  let fee: number = Math.floor(m / 1440) * 20000 + partFee(m % 1440);  if (kind === "disabled") {    fee = fee / 5; // 80% 할인. fee는 500의 배수라 나누어떨어진다  } else if (kind === "compact") {    if (m <= 1440) {      fee = fee / 2; // 하루를 넘기면 경차 할인이 없다    }  }  out.push(fee);}console.log(out.join("\n"));
JavaMain.java · 38 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException; public class Main {    // 하루가 안 되는 t분의 요금    static int partFee(int t) {        if (t <= 30) return 0;        int fee = 2000;        if (t > 60) {            fee += (t - 60 + 9) / 10 * 500; // 다 차지 않은 10분도 10분으로: 올림        }        return Math.min(fee, 20000);    }     public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(br.readLine().trim());        String[] cars = new String[n];        for (int i = 0; i < n; i++) cars[i] = br.readLine().trim();        StringBuilder out = new StringBuilder();        for (String car : cars) {            String[] p = car.split("\\s+");            String kind = p[0];            int m = Integer.parseInt(p[1]);            int fee = m / 1440 * 20000 + partFee(m % 1440);            if (kind.equals("disabled")) {                fee = fee / 5; // 80% 할인 = 5분의 1            } else if (kind.equals("compact")) {                if (m <= 1440) {                    fee = fee / 2; // 하루를 넘기면 경차 할인이 없다                }            }            out.append(fee).append('\n');        }        System.out.print(out);    }}
C#Main.cs · 46 lines
using System;using System.Collections.Generic;using System.Text; class Program{    // 하루가 안 되는 t분의 요금    static int PartFee(int t)    {        if (t <= 30) return 0;        int fee = 2000;        if (t > 60)        {            fee += (t - 60 + 9) / 10 * 500; // 다 차지 않은 10분도 10분으로: 올림        }        return Math.Min(fee, 20000);    }     static void Main()    {        int n = int.Parse(Console.ReadLine().Trim());        var cars = new List<string>();        for (int i = 0; i < n; i++) cars.Add(Console.ReadLine().Trim());        var out_ = new StringBuilder();        foreach (string car in cars)        {            string[] p = car.Split(' ', StringSplitOptions.RemoveEmptyEntries);            string kind = p[0];            int m = int.Parse(p[1]);            int fee = m / 1440 * 20000 + PartFee(m % 1440);            if (kind == "disabled")            {                fee = fee / 5; // 80% 할인 = 5분의 1            }            else if (kind == "compact")            {                if (m <= 1440)                {                    fee = fee / 2; // 하루를 넘기면 경차 할인이 없다                }            }            out_.Append(fee).Append('\n');        }        Console.Write(out_);    }}
C++main.cpp · 33 lines
#include <algorithm>#include <iostream>#include <string>#include <utility>#include <vector> // 하루가 안 되는 t분의 요금int partFee(int t) {    if (t <= 30) return 0;    int fee = 2000;    if (t > 60) {        fee += (t - 60 + 9) / 10 * 500; // 다 차지 않은 10분도 10분으로: 올림    }    return std::min(fee, 20000);} int main() {    int n;    std::cin >> n;    std::vector<std::pair<std::string, int>> cars(n);    for (auto& car : cars) std::cin >> car.first >> car.second;    for (const auto& [kind, m] : cars) {        int fee = m / 1440 * 20000 + partFee(m % 1440);        if (kind == "disabled") {            fee = fee / 5; // 80% 할인 = 5분의 1        } else if (kind == "compact") {            if (m <= 1440) {                fee = fee / 2; // 하루를 넘기면 경차 할인이 없다            }        }        std::cout << fee << '\n';    }}
Cmain.c · 32 lines
#include <stdio.h>#include <string.h> /* 하루가 안 되는 t분의 요금 */int part_fee(int t) {    if (t <= 30) return 0;    int fee = 2000;    if (t > 60) {        fee += (t - 60 + 9) / 10 * 500; /* 다 차지 않은 10분도 10분으로: 올림 */    }    return fee < 20000 ? fee : 20000;} int main(void) {    int n;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        char kind[16];        int m;        scanf("%15s %d", kind, &m);        int fee = m / 1440 * 20000 + part_fee(m % 1440);        if (strcmp(kind, "disabled") == 0) {            fee = fee / 5; /* 80% 할인 = 5분의 1 */        } else if (strcmp(kind, "compact") == 0) {            if (m <= 1440) {                fee = fee / 2; /* 하루를 넘기면 경차 할인이 없다 */            }        }        printf("%d\n", fee);    }    return 0;}
Craykonmain.cray · 39 lines
// 하루가 안 되는 t분의 요금부분요금(t){    if (t <= 30)    {        return 0;    }    요금 = 2000;    if (t > 60)    {        요금 += Mathf.Ceil((t - 60) / 10) * 500; // /는 실수 나눗셈이라 올림으로 묶음 수를 센다    }    return Mathf.Min(요금, 20000);} n = Console.ReadLine().ToNumber();차들 = [];for (i = 0; i < n; i++){    차들.Add(Console.ReadLine().Trim().Split());}foreach (차 in 차들){    종류 = 차[0];    m = 차[1].ToNumber();    요금 = Mathf.Quot(m, 1440) * 20000 + 부분요금(m % 1440);    if (종류 == "disabled")    {        요금 = 요금 / 5; // 80% 할인. 요금은 500의 배수라 나누어떨어진다    }    else if (종류 == "compact")    {        if (m <= 1440)        {            요금 = 요금 / 2; // 하루를 넘기면 경차 할인이 없다        }    }    Console.WriteLine(요금);}