{ }codelper
roundingrunning totalsP0407

This year's savings goal rate

Wrapping up the year, you sort out this year's savings record.
This year's savings goal is G won.
The first line gives G.
The second line gives the 12 amounts saved in each month from January to December, in order, separated by single spaces.
For each month, compute the running total c (the sum of all amounts saved from January through that month) and the achievement rate p.
The achievement rate p is the exact value of c × 100 ÷ G rounded to the nearest integer.
If the fractional part is exactly 0.5, round up to the larger integer (12.5 → 13, 32.5 → 33).
p may be greater than 100.
From January to December, print one line per month with the month number, c and p separated by single spaces, with % right after p (for example 1 100 13%).
On the last line, print the number of the first month in which c became G or more.
If there is no such month, print -.
Even if the rounded p is 100, the goal is not reached in that month while c is less than G.
Amounts are given with digits only, without commas, and printed numbers are written with digits only, without commas.

Input
1 ≤ G ≤ 10,000,000, 0 ≤ each monthly amount ≤ 1,000,000 (integers)
Output
12 lines of month number, running total and rate (followed by %), then the first month that reached the goal, or -
Example 1 · stdin
800
100 0 60 100 100 100 50 87 100 50 30 20
stdout
1 100 13%
2 100 13%
3 160 20%
4 260 33%
5 360 45%
6 460 58%
7 510 64%
8 597 75%
9 697 87%
10 747 93%
11 777 97%
12 797 100%
-
Example 2 · stdin
3
1 1 1 1 1 1 1 1 1 1 1 1
stdout
1 1 33%
2 2 67%
3 3 100%
4 4 133%
5 5 167%
6 6 200%
7 7 233%
8 8 267%
9 9 300%
10 10 333%
11 11 367%
12 12 400%
3
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

This problem takes all 8 languages.
Trap one is the exact half.
The task rounds 12.5 up to 13, but Python round(x) and C# Math.Round(x) (default MidpointRounding.ToEven) send halves to the even side, so 12.5 → 12 and 32.5 → 32.
In C#, use Math.Round(x, MidpointRounding.AwayFromZero).
The Python solution avoids floats and computes (c * 200 + G) // (2 * G), i.e. 'double it, add one, take the quotient by two', in integers.
Java Math.round, JS/TS Math.round, C lround, C++ std::lround and Craykon Mathf.Round all send halves up, so use them as they are (the rate is never negative, so 'toward +∞' and 'away from zero' agree).
Values that are not exact halves are at least 1/(2G) away from a half, so tiny floating-point errors cannot flip the rounding.
Trap two is integer division.
In C, C++, Java and C#, c * 100 / G divides int by int and keeps only the quotient, so Java Math.round(c * 100 / G) has already dropped the fraction before rounding: 12.5 becomes 12 and 99.625 becomes 99.
Make one side a double, as in c * 100.0 / G.
Python /, JS/TS / and Craykon / always divide into decimals (in Craykon, use Mathf.Quot for the quotient only).
c × 100 is at most 1.2 billion, so it fits in an int.
Trap three is the goal check.
In example 1, December's 99.625% is printed as 100%, but 797 is less than 800, so the answer is -.
Check c >= G, not the rounded p.

Pythonmain.py · 13 lines
g = int(input())amounts = list(map(int, input().split())) c = 0reached = "-"for month, a in enumerate(amounts, start=1):    c += a    # c * 100 / g를 반올림(딱 반이면 올림): 정수만으로 (2x + 1) // 2를 계산한다    p = (c * 200 + g) // (2 * g)    print(f"{month} {c} {p}%")    if reached == "-" and c >= g:  # 달성은 반올림 전의 금액으로 판단한다        reached = str(month)print(reached)
JavaScriptmain.js · 16 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const g = Number(lines[0]);const amounts = lines[1].trim().split(" ").map(Number); let c = 0;let reached = "-";const out = [];amounts.forEach((a, i) => {  c += a;  // Math.round는 딱 반(12.5)을 큰 쪽(13)으로 올린다  const p = Math.round((c * 100) / g);  out.push(`${i + 1} ${c} ${p}%`);  if (reached === "-" && c >= g) reached = String(i + 1); // 반올림 전의 금액으로 판단});out.push(reached);console.log(out.join("\n"));
TypeScriptmain.ts · 17 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const g: number = Number(lines[0]);const amounts: number[] = lines[1].trim().split(" ").map(Number); let c = 0;let reached = "-";const out: string[] = [];amounts.forEach((a, i) => {  c += a;  // Math.round는 딱 반(12.5)을 큰 쪽(13)으로 올린다  const p = Math.round((c * 100) / g);  out.push(`${i + 1} ${c} ${p}%`);  if (reached === "-" && c >= g) reached = String(i + 1); // 반올림 전의 금액으로 판단});out.push(reached);console.log(out.join("\n"));
JavaMain.java · 22 lines
import java.io.*;import java.util.StringTokenizer; public class Main {    public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int g = Integer.parseInt(in.readLine().trim());        StringTokenizer st = new StringTokenizer(in.readLine());        int c = 0;        String reached = "-";        StringBuilder out = new StringBuilder();        for (int month = 1; month <= 12; month++) {            c += Integer.parseInt(st.nextToken());            // 100.0으로 곱해야 실수 나눗셈이 된다. Math.round는 딱 반을 큰 쪽으로 보내 12.5 → 13            long p = Math.round(c * 100.0 / g);            out.append(month).append(' ').append(c).append(' ').append(p).append("%\n");            if (reached.equals("-") && c >= g) reached = String.valueOf(month); // 반올림 전의 금액으로 판단        }        out.append(reached).append('\n');        System.out.print(out);    }}
C#Main.cs · 24 lines
using System;using System.Text; class Program{    static void Main()    {        int g = int.Parse(Console.ReadLine().Trim());        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int c = 0;        string reached = "-";        var sb = new StringBuilder();        for (int month = 1; month <= 12; month++)        {            c += int.Parse(parts[month - 1]);            // Math.Round의 기본은 짝수 쪽이라 딱 반을 올리려면 AwayFromZero를 준다            long p = (long)Math.Round(c * 100.0 / g, MidpointRounding.AwayFromZero);            sb.Append(month).Append(' ').Append(c).Append(' ').Append(p).Append("%\n");            if (reached == "-" && c >= g) reached = month.ToString(); // 반올림 전의 금액으로 판단        }        sb.Append(reached).Append('\n');        Console.Write(sb);    }}
C++main.cpp · 20 lines
#include <cmath>#include <iostream>#include <string> int main() {    int g;    std::cin >> g;    int c = 0;    std::string reached = "-";    for (int month = 1; month <= 12; month++) {        int a;        std::cin >> a;        c += a;        // 100.0으로 곱해 실수로 나누고, std::lround는 딱 반을 0에서 먼 쪽(12.5 → 13)으로 보낸다        long p = std::lround(c * 100.0 / g);        std::cout << month << ' ' << c << ' ' << p << "%\n";        if (reached == "-" && c >= g) reached = std::to_string(month); // 반올림 전의 금액으로 판단    }    std::cout << reached << '\n';}
Cmain.c · 19 lines
#include <math.h>#include <stdio.h> int main(void) {    int g, a;    if (scanf("%d", &g) != 1) return 0;    int c = 0, reached = 0;    for (int month = 1; month <= 12; month++) {        scanf("%d", &a);        c += a;        /* 100.0으로 곱해 실수로 나누고, lround는 딱 반을 0에서 먼 쪽(12.5 → 13)으로 보낸다 */        long p = lround(c * 100.0 / g);        printf("%d %d %ld%%\n", month, c, p);        if (reached == 0 && c >= g) reached = month; /* 반올림 전의 금액으로 판단 */    }    if (reached == 0) printf("-\n");    else printf("%d\n", reached);    return 0;}
Craykonmain.cray · 18 lines
g = Console.ReadLine().ToNumber();amounts = Console.ReadNumbers();c = 0;reached = "-";month = 0;foreach (a in amounts){    month++;    c = c + a;    // 수학.반올림은 딱 반(12.5)을 0에서 먼 쪽(13)으로 보낸다    p = Mathf.Round(c * 100 / g);    Console.WriteLine(month + " " + c + " " + p + "%");    if (reached == "-" && c >= g)    {        reached = "" + month;    }}Console.WriteLine(reached);