An amusement park sets its admission fee from a visitor's age in completed years.
The first line gives today's date, the second line the number of visitors n, and each of the next n lines one visitor's date of birth.
All dates have the form YYYY-MM-DD (two digits for month and day), and a date of birth is never after today.
The age is (today's year - year of birth), minus 1 if this year's birthday has not come yet.
This year's birthday has not come yet if today's month is before the birth month, or the two months are the same and today's day is before the birth day.
By this rule, a person born on February 29 becomes one year older on March 1 in common years.
The fee is 0 won for age 2 or under, 6,000 won for 3 to 12, 9,000 won for 13 to 18, 15,000 won for 19 to 64, and 7,000 won for 65 or over.
However, if today's month and day are both the same as the month and day of birth, that is, today is the birthday, the fee is 0 won whatever the age.
For each visitor, in input order, print one line with the age and the fee, separated by one space.
On the last line print the sum of the fees.
Input
1900-01-01 ≤ date of birth ≤ today ≤ 2100-12-31, 1 ≤ n ≤ 1,000, every date is a real calendar date
Output
One line per visitor (age, fee), then the sum of the fees
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 comparison that asks "has the birthday passed?" is the whole problem. The usual mistake is asking month and day separately, tm >= bm && td >= bd. On May 3 a person born on April 20 has passed the month, but the day (3 < 20) makes it look like the birthday is still to come. The right condition is "the month is earlier, or the month is the same and the day is earlier": tm < bm || (tm == bm && td < bd) in seven languages and tm < bm or (tm == bm and td < bd) in Python (Craykon also uses || and &&). Python tuples (tm, td) < (bm, bd) and C++ std::tie(tm, td) < std::tie(bm, bd) compare from the first item onward, so they write this condition in one go. JS/TS arrays have no such comparison. [tm, td] < [bm, bd] turns both arrays into the strings "10,1" and "9,30" and compares them in dictionary order, so October 1 comes out before September 30 (TS does not reject this expression). Java and C# tuples and arrays have no < either, so compare item by item or turn the date into one number such as month × 100 + day. Comparing the date strings "MM-DD" also works, because month and day have two digits, so dictionary order is date order: < in Python, JS, TS and Craykon, compareTo in Java, string.CompareOrdinal in C#, < on C++ std::string. But in C, comparing two char arrays with < compares their memory addresses, not their contents, so you must use strcmp.
Pythonmain.py · 31 lines
def parse(s): y, m, d = map(int, s.split("-")) return y, m, ddef price(age): if age <= 2: return 0 elif age <= 12: return 6000 elif age <= 18: return 9000 elif age <= 64: return 15000 else: return 7000ty, tm, td = parse(input().strip())n = int(input())total = 0for _ in range(n): by, bm, bd = parse(input().strip()) age = ty - by # 튜플은 앞 칸부터 차례로 비교한다: 월이 같을 때만 일을 본다 if (tm, td) < (bm, bd): age -= 1 fee = 0 if (tm, td) == (bm, bd) else price(age) # 생일 당일은 무료 print(age, fee) total += feeprint(total)
JavaScriptmain.js · 24 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const parse = (s) => s.trim().split("-").map(Number);const price = (age) => { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000;};const [ty, tm, td] = parse(lines[0]);const n = Number(lines[1]);const out = [];let total = 0;for (let i = 0; i < n; i++) { const [by, bm, bd] = parse(lines[2 + i]); // 배열끼리 <로 비교하면 문자열 "10,1" < "9,30"으로 비교된다 → 칸마다 직접 비교한다 const beforeBirthday = tm < bm || (tm === bm && td < bd); const age = ty - by - (beforeBirthday ? 1 : 0); const fee = tm === bm && td === bd ? 0 : price(age); // 생일 당일은 무료 out.push(`${age} ${fee}`); total += fee;}out.push(String(total));console.log(out.join("\n"));
TypeScriptmain.ts · 25 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const parse = (s: string): number[] => s.trim().split("-").map(Number);const price = (age: number): number => { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000;};const [ty, tm, td] = parse(lines[0]);const n = Number(lines[1]);const out: string[] = [];let total = 0;for (let i = 0; i < n; i++) { const [by, bm, bd] = parse(lines[2 + i]); // 배열끼리 <로 비교하면 문자열 "10,1" < "9,30"으로 비교된다 → 칸마다 직접 비교한다 const beforeBirthday = tm < bm || (tm === bm && td < bd); const age = ty - by - (beforeBirthday ? 1 : 0); const fee = tm === bm && td === bd ? 0 : price(age); // 생일 당일은 무료 out.push(`${age} ${fee}`); total += fee;}out.push(String(total));console.log(out.join("\n"));
JavaMain.java · 36 lines
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { static int[] parse(String s) { String[] p = s.trim().split("-"); return new int[] {Integer.parseInt(p[0]), Integer.parseInt(p[1]), Integer.parseInt(p[2])}; } static int price(int age) { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000; } public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int[] t = parse(in.readLine()); int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); long total = 0; for (int i = 0; i < n; i++) { int[] b = parse(in.readLine()); // 월이 앞서거나, 월이 같고 일이 앞서면 아직 생일 전이다 boolean beforeBirthday = t[1] < b[1] || (t[1] == b[1] && t[2] < b[2]); int age = t[0] - b[0] - (beforeBirthday ? 1 : 0); int fee = (t[1] == b[1] && t[2] == b[2]) ? 0 : price(age); // 생일 당일은 무료 out.append(age).append(' ').append(fee).append('\n'); total += fee; } out.append(total).append('\n'); System.out.print(out); }}
C#Main.cs · 35 lines
using System;using System.Linq;using System.Text;class Program{ static int Price(int age) { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000; } static void Main() { int[] t = Console.ReadLine().Trim().Split('-').Select(int.Parse).ToArray(); int n = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); long total = 0; for (int i = 0; i < n; i++) { int[] b = Console.ReadLine().Trim().Split('-').Select(int.Parse).ToArray(); // 튜플은 앞 칸부터 비교하지만 C#의 튜플에는 <가 없다 → 칸마다 직접 비교한다 bool beforeBirthday = t[1] < b[1] || (t[1] == b[1] && t[2] < b[2]); int age = t[0] - b[0] - (beforeBirthday ? 1 : 0); int fee = (t[1] == b[1] && t[2] == b[2]) ? 0 : Price(age); // 생일 당일은 무료 sb.Append(age).Append(' ').Append(fee).Append('\n'); total += fee; } sb.Append(total).Append('\n'); Console.Write(sb); }}
C++main.cpp · 29 lines
#include <iostream>#include <string>#include <tuple>int price(int age) { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000;}int main() { char dash; int ty, tm, td, n; std::cin >> ty >> dash >> tm >> dash >> td >> n; long long total = 0; for (int i = 0; i < n; i++) { int by, bm, bd; std::cin >> by >> dash >> bm >> dash >> bd; // std::tie로 묶으면 튜플처럼 앞 칸부터 차례로 비교한다 bool beforeBirthday = std::tie(tm, td) < std::tie(bm, bd); int age = ty - by - (beforeBirthday ? 1 : 0); int fee = (tm == bm && td == bd) ? 0 : price(age); // 생일 당일은 무료 std::cout << age << ' ' << fee << '\n'; total += fee; } std::cout << total << '\n';}
Cmain.c · 28 lines
#include <stdio.h>int price(int age) { if (age <= 2) return 0; else if (age <= 12) return 6000; else if (age <= 18) return 9000; else if (age <= 64) return 15000; else return 7000;}int main(void) { int ty, tm, td, n; scanf("%d-%d-%d", &ty, &tm, &td); scanf("%d", &n); long total = 0; for (int i = 0; i < n; i++) { int by, bm, bd; scanf("%d-%d-%d", &by, &bm, &bd); /* 월이 앞서거나, 월이 같고 일이 앞서면 아직 생일 전이다 */ int before_birthday = tm < bm || (tm == bm && td < bd); int age = ty - by - (before_birthday ? 1 : 0); int fee = (tm == bm && td == bd) ? 0 : price(age); /* 생일 당일은 무료 */ printf("%d %d\n", age, fee); total += fee; } printf("%ld\n", total); return 0;}
Craykonmain.cray · 48 lines
날짜(줄){ 칸 = 줄.Trim().Split("-"); return [칸[0].ToNumber(), 칸[1].ToNumber(), 칸[2].ToNumber()];}요금(age){ if (age <= 2) { return 0; } else if (age <= 12) { return 6000; } else if (age <= 18) { return 9000; } else if (age <= 64) { return 15000; } return 7000;}t = 날짜(Console.ReadLine());n = Console.ReadLine().ToNumber();total = 0;for (i = 0; i < n; i++){ b = 날짜(Console.ReadLine()); age = t[0] - b[0]; // 월이 앞서거나, 월이 같고 일이 앞서면 아직 생일 전이다 if (t[1] < b[1] || t[1] == b[1] && t[2] < b[2]) { age -= 1; } fee = 요금(age); if (t[1] == b[1] && t[2] == b[2]) { fee = 0; // 생일 당일은 무료 } Console.WriteLine(age + " " + fee); total += fee;}Console.WriteLine(total);