{ }codelper
reading n linesfloating-point precisiontype conversionP0043

Exactly the money you have

The first line gives the number of items n, and the next n lines give one price each.
The last line gives the money you have, m.
Every price and m is written with exactly two digits after the decimal point (e.g.
0.10, 12.30, 7.05).
Print "yes" if the sum of all prices is exactly equal to m, otherwise "no".

Input
1 ≤ n ≤ 1,000, 0.01 ≤ price ≤ 10,000.00, 0.00 ≤ m ≤ 10,000,000.00
Output
"yes" or "no"
Example 1 · stdin
2
0.10
0.20
0.30
stdout
yes
Example 2 · stdin
3
1.50
2.25
0.29
4.05
stdout
no
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

Adding the prices as doubles and comparing with == is wrong.
0.1 and 0.2 cannot be written exactly in binary, so 0.1 + 0.2 is 0.30000000000000004.
Python float, JS/TS number and Java/C/C++ double are all the same IEEE 754 double, so 0.1 + 0.2 == 0.3 is false in all of them.
Craykon numbers are JS doubles too, so the comparison is false, but Console.WriteLine rounds at the 9th decimal and shows 0.3, so you cannot see the error.
Converting by multiplying by 100 has its own trap: (int)(x * 100) in C/C++/Java/C#, Python int() and JS Math.trunc cut toward zero, and 0.29 * 100 is 28.999999999999996, which becomes 28 — round instead (round, Math.round).
A tolerance like abs(a - b) < 1e-9 is risky too: near ten million the gap between neighbouring doubles is itself about 1.9e-9, so equal amounts can still look different.
The safest way is to read whole cents from the start: drop the point from the text ("12.34" → 1234) — Python int(s.replace(".", "")), Java Long.parseLong, JS/TS Number, C scanf("%lld.%lld", &w, &f), Craykon line.Replace(".", "").ToNumber().
The largest total, one billion cents, fits a 32-bit int (about 2.1 billion), but for headroom use long long in C/C++ and long in Java/C#.
C# has a decimal floating type, decimal; summing with decimal.Parse makes 0.1 + 0.2 == 0.3 true (Java does the same with BigDecimal, Python with decimal.Decimal).

Pythonmain.py · 10 lines
# 값은 소수 두 자리로 정확히 주어지므로 점을 빼면 바로 '센트(1/100)' 단위 정수다def cents(s):    return int(s.strip().replace(".", "")) n = int(input())total = 0for _ in range(n):    total += cents(input())m = cents(input())print("yes" if total == m else "no")
JavaScriptmain.js · 8 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");// "12.34" → 1234: 점을 빼면 센트 단위 정수가 된다const cents = (s) => Number(s.trim().replace(".", ""));const n = Number(lines[0]);let total = 0;for (let i = 1; i <= n; i++) total += cents(lines[i]);const m = cents(lines[n + 1]);console.log(total === m ? "yes" : "no");
TypeScriptmain.ts · 9 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");// "12.34" → 1234: 점을 빼면 센트 단위 정수가 된다const cents = (s: string): number => Number(s.trim().replace(".", ""));const n: number = Number(lines[0]);let total = 0;for (let i = 1; i <= n; i++) total += cents(lines[i]);const m: number = cents(lines[n + 1]);console.log(total === m ? "yes" : "no");
JavaMain.java · 19 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class Main {    // "12.34" → 1234: 점을 빼면 센트 단위 정수가 된다    static long cents(String s) {        return Long.parseLong(s.trim().replace(".", ""));    }     public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(in.readLine().trim());        long total = 0;        for (int i = 0; i < n; i++) total += cents(in.readLine());        long m = cents(in.readLine());        System.out.println(total == m ? "yes" : "no");    }}
C#Main.cs · 14 lines
using System; class Program{    static void Main()    {        // C#의 decimal은 십진수라 0.1 + 0.2 == 0.3 이 참이다        int n = int.Parse(Console.ReadLine());        decimal total = 0m;        for (int i = 0; i < n; i++) total += decimal.Parse(Console.ReadLine());        decimal m = decimal.Parse(Console.ReadLine());        Console.WriteLine(total == m ? "yes" : "no");    }}
C++main.cpp · 21 lines
#include <iostream>#include <string> // "12.34" → 1234: 점을 빼면 센트 단위 정수가 된다long long cents(std::string s) {    s.erase(s.find('.'), 1);    return std::stoll(s);} int main() {    int n;    std::cin >> n;    long long total = 0;    std::string s;    for (int i = 0; i < n; i++) {        std::cin >> s;        total += cents(s);    }    std::cin >> s;    std::cout << (total == cents(s) ? "yes" : "no") << "\n";}
Cmain.c · 18 lines
#include <stdio.h> /* "12.34"를 정수 12와 34로 따로 읽는다 — 소수는 정확히 두 자리라 34는 센트다 */long long read_cents(void) {    long long whole, frac;    scanf("%lld.%lld", &whole, &frac);    return whole * 100 + frac;} int main(void) {    int n;    scanf("%d", &n);    long long total = 0;    for (int i = 0; i < n; i++) total += read_cents();    long long m = read_cents();    puts(total == m ? "yes" : "no");    return 0;}
Craykonmain.cray · 14 lines
센트(줄){    // "12.34" → "1234" → 1234    return 줄.Trim().Replace(".", "").ToNumber();} n = Console.ReadLine().ToNumber();합 = 0;for (i = 0; i < n; i++){    합 += 센트(Console.ReadLine());}m = 센트(Console.ReadLine());Console.WriteLine(합 == m ? "yes" : "no");