은행에 돈을 맡기고 매달 이자를 받는다.
한 줄에 처음 맡긴 돈 P(달러), 연 이율 R(퍼센트), 개월 수 m이 빈칸으로 나뉘어 주어진다.
P와 R은 소수점 아래를 정확히 두 자리로 쓴 수다(예: 1000.00, 3.25).
매달 말에 그달의 이자를 잔액 × R ÷ 100 ÷ 12로 계산한다.
이자는 센트(0.01달러) 단위로 반올림하는데, 센트 아래가 정확히 0.5센트이면 올린다.
반올림한 이자를 잔액에 더하고, 다음 달 이자는 늘어난 잔액으로 계산한다.
첫째 달부터 m째 달까지 달마다 한 줄씩 "달 번호(1부터 m까지), 그달 이자, 이자를 더한 잔액"을 빈칸 하나로 띄어 출력하라.
마지막 줄에는 m달 동안 받은 이자의 합을 출력하라.
돈은 모두 소수점 아래를 정확히 두 자리로 쓴다(예: 5.00, 0.03).
입력
0.01 ≤ P ≤ 1,000,000.00, 0.00 ≤ R ≤ 20.00, 1 ≤ m ≤ 120
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
돈 계산은 실수(double)를 쓰지 않고 정수 센트로 하는 것이 정답이에요. 0.01이나 1005.03 같은 소수는 이진수로 정확히 적을 수 없어서, float/double로 120달 동안 더하고 반올림하면 어느 달엔가 1센트가 어긋날 수 있어요. 그래서 입력 문자열 "1000.05"를 점을 기준으로 나눠 정수 100005센트로 읽어요: Python s.split("."), JS·TS s.split("."), Java s.split("\\.")(정규식이라 점을 이스케이프), C# s.Split('.'), C scanf("%lld.%lld"), C++ s.find('.')와 std::stoll, 크레이콘 줄.바꾸기(".", "").수로변환()이에요. 그러면 정확한 이자는 잔액 × 이율 ÷ 120000센트이고, 0.5를 올리는 반올림은 정수만으로 (2 × 잔액 × 이율 + 120000) ÷ 240000의 몫이에요. 반올림 함수의 함정: Python round와 C# Math.Round는 기본이 짝수 쪽 반올림(banker's rounding)이라 252.5가 252가 돼요. C# Math.Round(x, MidpointRounding.AwayFromZero), Java Math.round, JS·TS Math.round, C·C++ round, 크레이콘 수학.반올림은 0.5를 올려요(음수 쪽 규칙은 언어마다 달라요). 그래도 그 함수들은 실수를 받으니, 정확히 0.5인지 알아보려면 정수로 계산하는 편이 안전해요. 그냥 정수 나눗셈 잔액 * 이율 / 120000만 하면 반올림이 아니라 버림이 돼요. 크기도 봐야 해요. 2 × 잔액 × 이율은 3×10^12까지 커져서 32비트 int를 넘으니 Java·C#은 long, C·C++은 long long이에요. Python 정수는 크기 제한이 없고, JS·TS·크레이콘의 수는 2^53까지 정수가 정확해서 괜찮아요. 출력은 몫과 나머지로 "5.03"을 만들고, 나머지는 두 자리로 앞에 0을 채워요: Python f"{c % 100:02d}", JS·TS padStart(2, "0"), Java String.format("%02d"), C# :D2, C %02lld, 크레이콘 앞채우기(2, "0").
Pythonmain.py · 21 줄
def hundredths(s): # "1000.05" → 100005 (소수점 아래는 언제나 두 자리) whole, frac = s.split(".") return int(whole) * 100 + int(frac)def money(c): return f"{c // 100}.{c % 100:02d}"p, r, m = input().split()bal = hundredths(p) # 잔액(센트)rate = hundredths(r) # 연 이율(0.01% 단위): 3.25% → 325total = 0for month in range(1, int(m) + 1): # 정확한 이자 = bal × rate / 120000 센트. 반올림(0.5는 올림) = (2 × 분자 + 분모) // (2 × 분모) interest = (2 * bal * rate + 120000) // 240000 bal += interest total += interest print(month, money(interest), money(bal))print(money(total))
JavaScriptmain.js · 20 줄
const [p, r, m] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);// "1000.05" → 100005 (소수점 아래는 언제나 두 자리). 정수로만 계산하면 실수 오차가 없다const hundredths = (s) => { const [whole, frac] = s.split("."); return Number(whole) * 100 + Number(frac);};const money = (c) => Math.floor(c / 100) + "." + String(c % 100).padStart(2, "0");let bal = hundredths(p); // 잔액(센트)const rate = hundredths(r); // 연 이율(0.01% 단위)let total = 0;const out = [];for (let month = 1; month <= Number(m); month++) { // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 최대 3×10^12라 number로 정확하다 const interest = Math.floor((2 * bal * rate + 120000) / 240000); bal += interest; total += interest; out.push(month + " " + money(interest) + " " + money(bal));}out.push(money(total));console.log(out.join("\n"));
TypeScriptmain.ts · 21 줄
import { readFileSync } from "node:fs";const [p, r, m]: string[] = readFileSync(0, "utf8").trim().split(/\s+/);// "1000.05" → 100005 (소수점 아래는 언제나 두 자리). 정수로만 계산하면 실수 오차가 없다const hundredths = (s: string): number => { const [whole, frac] = s.split("."); return Number(whole) * 100 + Number(frac);};const money = (c: number): string => Math.floor(c / 100) + "." + String(c % 100).padStart(2, "0");let bal: number = hundredths(p); // 잔액(센트)const rate: number = hundredths(r); // 연 이율(0.01% 단위)let total = 0;const out: string[] = [];for (let month = 1; month <= Number(m); month++) { // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 최대 3×10^12라 number로 정확하다 const interest: number = Math.floor((2 * bal * rate + 120000) / 240000); bal += interest; total += interest; out.push(month + " " + money(interest) + " " + money(bal));}out.push(money(total));console.log(out.join("\n"));
JavaMain.java · 31 줄
import java.util.Scanner;public class Main { // "1000.05" → 100005 (소수점 아래는 언제나 두 자리) static long hundredths(String s) { String[] parts = s.split("\\."); return Long.parseLong(parts[0]) * 100 + Long.parseLong(parts[1]); } static String money(long c) { return c / 100 + "." + String.format("%02d", c % 100); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long bal = hundredths(sc.next()); // 잔액(센트) long rate = hundredths(sc.next()); // 연 이율(0.01% 단위) int m = sc.nextInt(); long total = 0; StringBuilder out = new StringBuilder(); for (int month = 1; month <= m; month++) { // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 곱이 int를 넘으니 long long interest = (2 * bal * rate + 120000) / 240000; bal += interest; total += interest; out.append(month).append(' ').append(money(interest)).append(' ').append(money(bal)).append('\n'); } out.append(money(total)).append('\n'); System.out.print(out); }}
C#Main.cs · 34 줄
using System;using System.Text;class Program{ // "1000.05" → 100005 (소수점 아래는 언제나 두 자리) static long Hundredths(string s) { string[] parts = s.Split('.'); return long.Parse(parts[0]) * 100 + long.Parse(parts[1]); } static string Money(long c) => $"{c / 100}.{c % 100:D2}"; static void Main() { string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); long bal = Hundredths(t[0]); // 잔액(센트) long rate = Hundredths(t[1]); // 연 이율(0.01% 단위) int m = int.Parse(t[2]); long total = 0; var sb = new StringBuilder(); for (int month = 1; month <= m; month++) { // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. Math.Round는 기본이 짝수 쪽이라 쓰지 않는다 long interest = (2 * bal * rate + 120000) / 240000; bal += interest; total += interest; sb.Append(month).Append(' ').Append(Money(interest)).Append(' ').Append(Money(bal)).Append('\n'); } sb.Append(Money(total)).Append('\n'); Console.Write(sb); }}
C++main.cpp · 31 줄
#include <iomanip>#include <iostream>#include <string>// "1000.05" → 100005 (소수점 아래는 언제나 두 자리)long long hundredths(const std::string& s) { auto dot = s.find('.'); return std::stoll(s.substr(0, dot)) * 100 + std::stoll(s.substr(dot + 1));}std::string money(long long c) { std::string cents = std::to_string(c % 100); return std::to_string(c / 100) + "." + (cents.size() < 2 ? "0" : "") + cents;}int main() { std::string p, r; int m; std::cin >> p >> r >> m; long long bal = hundredths(p); // 잔액(센트) long long rate = hundredths(r); // 연 이율(0.01% 단위) long long total = 0; for (int month = 1; month <= m; month++) { // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 곱이 int를 넘으니 long long long long interest = (2 * bal * rate + 120000) / 240000; bal += interest; total += interest; std::cout << month << ' ' << money(interest) << ' ' << money(bal) << '\n'; } std::cout << money(total) << '\n';}
Cmain.c · 34 줄
#include <stdio.h>/* "1000.05"를 정수 1000과 05로 따로 읽는다 — 소수는 언제나 두 자리다 */long long read_hundredths(void) { long long whole, frac; scanf("%lld.%lld", &whole, &frac); return whole * 100 + frac;}void print_money(long long c) { printf("%lld.%02lld", c / 100, c % 100);}int main(void) { long long bal = read_hundredths(); /* 잔액(센트) */ long long rate = read_hundredths(); /* 연 이율(0.01% 단위) */ int m; scanf("%d", &m); long long total = 0; for (int month = 1; month <= m; month++) { /* 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 곱이 int를 넘으니 long long */ long long interest = (2 * bal * rate + 120000) / 240000; bal += interest; total += interest; printf("%d ", month); print_money(interest); putchar(' '); print_money(bal); putchar('\n'); } print_money(total); putchar('\n'); return 0;}
Craykonmain.cray · 25 줄
백분(줄){ // "1000.05" → "100005" → 100005 (소수점 아래는 언제나 두 자리) 돌려주기 줄.바꾸기(".", "").수로변환();}돈(c){ 돌려주기 수학.몫(c, 100) + "." + ("" + c % 100).앞채우기(2, "0");}t = 콘솔.줄읽기().나누기(" ");bal = 백분(t[0]); // 잔액(센트)rate = 백분(t[1]); // 연 이율(0.01% 단위)m = t[2].수로변환();total = 0;반복 (month = 1; month <= m; month++){ // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 최대 3×10^12라 수가 정확하다 interest = 수학.몫(2 * bal * rate + 120000, 240000); bal += interest; total += interest; 콘솔.쓰기(month + " " + 돈(interest) + " " + 돈(bal));}콘솔.쓰기(돈(total));