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 行
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 行
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 行
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 行
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 行
#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 行
#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;}