You keep money in a bank and receive interest every month.
One line gives the starting deposit P (dollars), the yearly rate R (percent) and the number of months m, separated by spaces.
P and R are written with exactly two digits after the decimal point (for example 1000.00, 3.25).
At the end of each month the interest for that month is balance × R ÷ 100 ÷ 12.
The interest is rounded to cents (0.01 dollar), and a remainder of exactly half a cent is rounded up.
Add the rounded interest to the balance; the next month's interest uses the increased balance.
For each month from the first to the m-th, print one line with "month number (1 to m), that month's interest, balance after adding it", separated by single spaces.
On the last line print the total interest received over the m months.
Write every amount with exactly two digits after the decimal point (for example 5.00, 0.03).
Input
0.01 ≤ P ≤ 1,000,000.00, 0.00 ≤ R ≤ 20.00, 1 ≤ m ≤ 120
Output
m lines: month, interest, balance / last line: total interest (amounts with two decimals)
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 right way to count money is in integer cents, not floating-point doubles. Decimals such as 0.01 or 1005.03 cannot be written exactly in binary, so adding and rounding with float/double for 120 months can make some month one cent off. So read the input string "1000.05" by splitting it at the dot into the integer 100005 cents: Python s.split("."), JS/TS s.split("."), Java s.split("\\.") (a regex, so the dot is escaped), C# s.Split('.'), C scanf("%lld.%lld"), C++ s.find('.') with std::stoll, Craykon line.Replace(".", "").ToNumber(). Then the exact interest is balance × rate ÷ 120000 cents, and rounding half up with integers only is the quotient of (2 × balance × rate + 120000) ÷ 240000. The rounding-function trap: Python round and C# Math.Round default to round-half-to-even (banker's rounding), so 252.5 becomes 252. C# Math.Round(x, MidpointRounding.AwayFromZero), Java Math.round, JS/TS Math.round, C/C++ round and Craykon Mathf.Round round 0.5 up (their rules for negatives differ). Still, those functions take a double, so computing with integers is the safe way to know whether a value is exactly one half. Plain integer division balance * rate / 120000 truncates instead of rounding. Watch the size too. 2 × balance × rate reaches 3×10^12, beyond a 32-bit int, so Java and C# need long, C and C++ long long. Python integers have no limit, and JS, TS and Craykon numbers are exact integers up to 2^53, so they are fine. Build "5.03" from the quotient and remainder, padding the remainder to two digits with a leading zero: Python f"{c % 100:02d}", JS/TS padStart(2, "0"), Java String.format("%02d"), C# :D2, C %02lld, Craykon PadLeft(2, "0").
Pythonmain.py · 21 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
백분(줄){ // "1000.05" → "100005" → 100005 (소수점 아래는 언제나 두 자리) return 줄.Replace(".", "").ToNumber();}돈(c){ return Mathf.Quot(c, 100) + "." + ("" + c % 100).PadLeft(2, "0");}t = Console.ReadLine().Split(" ");bal = 백분(t[0]); // 잔액(센트)rate = 백분(t[1]); // 연 이율(0.01% 단위)m = t[2].ToNumber();total = 0;for (month = 1; month <= m; month++){ // 정확한 이자 = bal × rate / 120000 센트, 0.5는 올림. 최대 3×10^12라 수가 정확하다 interest = Mathf.Quot(2 * bal * rate + 120000, 240000); bal += interest; total += interest; Console.WriteLine(month + " " + 돈(interest) + " " + 돈(bal));}Console.WriteLine(돈(total));