和手里的钱正好相等吗
第一行给出物品数 n,接下来 n 行每行给出一个价格。
最后一行给出手里的钱 m。
所有价格和 m 都写成小数点后正好两位的数(例如 0.10、12.30、7.05)。
如果所有价格之和正好等于 m 输出 "是",否则输出 "否"。
- 输入
- 1 ≤ n ≤ 1,000,0.01 ≤ 价格 ≤ 10,000.00,0.00 ≤ m ≤ 10,000,000.00
- 输出
- "是" 或 "否"
2 0.10 0.20 0.30
是
3 1.50 2.25 0.29 4.05
否
第一行给出物品数 n,接下来 n 行每行给出一个价格。
最后一行给出手里的钱 m。
所有价格和 m 都写成小数点后正好两位的数(例如 0.10、12.30、7.05)。
如果所有价格之和正好等于 m 输出 "是",否则输出 "否"。
2 0.10 0.20 0.30
是
3 1.50 2.25 0.29 4.05
否
运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。
$ 就绪
请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。
把价格当作浮点数(double)相加再用 == 比较是错的。
0.1 和 0.2 无法用二进制精确表示,所以 0.1 + 0.2 是 0.30000000000000004。
Python float、JS/TS number、Java/C/C++ double 都是同一种 IEEE 754 双精度,所以 0.1 + 0.2 == 0.3 在它们中都为假。
Craykon 的数字也是 JS double,比较同样为假,但 控制台.输出 会在小数第 9 位四舍五入显示成 0.3,所以肉眼看不到误差。
乘以 100 转成整数也有陷阱:C/C++/Java/C# 的 (int)(x * 100)、Python int()、JS Math.trunc 都向 0 截断,而 0.29 * 100 是 28.999999999999996,会变成 28——要转换就用四舍五入(round、Math.round)。abs(a - b) < 1e-9 这样的容差也危险:在一千万附近,相邻两个 double 的间隔本身就约为 1.9e-9,相等的金额也会被判为不同。
最可靠的办法是一开始就按整数“分”来读:把文字里的点去掉("12.34" → 1234)即可——Python int(s.replace(".", ""))、Java Long.parseLong、JS/TS Number、C scanf("%lld.%lld", &w, &f)、Craykon 行.替换(".", "").转数字()。
最大总和 10 亿分能放进 32 位 int(约 21 亿),但为了留余地,C/C++ 用 long long,Java/C# 用 long。
C# 有十进制浮点类型 decimal,用 decimal.Parse 相加时 0.1 + 0.2 == 0.3 为真(Java 用 BigDecimal、Python 用 decimal.Decimal 也能做到)。
# 값은 소수 두 자리로 정확히 주어지므로 점을 빼면 바로 '센트(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("是" if total == m else "否")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 ? "是" : "否");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 ? "是" : "否");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 ? "是" : "否"); }}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 ? "是" : "否"); }}#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) ? "是" : "否") << "\n";}#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 ? "是" : "否"); return 0;}센트(줄){ // "12.34" → "1234" → 1234 返回 줄.去空白().替换(".", "").转数字();} n = 控制台.读行().转数字();합 = 0;循环 (i = 0; i < n; i++){ 합 += 센트(控制台.读行());}m = 센트(控制台.读行());控制台.输出(합 == m ? "是" : "否");