“没有值”和数字 0 不是一回事。 存放缺失折扣价的方法:Python None,JS null,TS number | null,Java Integer(int 放不了 null),C# int?,C++17 std::optional<int>,Craykon 空。 C 的 int 没有“无”,所以要另外放一个布尔值,比如 bool has_sale。 “没有就用另一个值”的一步写法也不同:JS、TS、C# 用 sale ?? full,C++ 用 sale.value_or(full)。 Python、Java、C、Craykon 没有 ??,要用 if sale is None、sale == null、sale == 空 这样的判断来分开处理。 三个陷阱:① Python sale or full、JS sale || full 不只把 null、也把 0 当作假,于是白送的物品(折扣价 0)被收了原价。 用 !sale、not sale 来数“没有”的个数,也会把 0 一起数进去。 ② 用 0 代替“无”,就和真正的折扣价 0 分不清。 C 的 atoi("-") 会悄悄返回 0。 ③ 不先判断是不是 - 就转成数字,JS Number("-") 会得到 NaN,总和也变成 NaN;Python int("-") 会因 ValueError、Java Integer.parseInt("-") 会因 NumberFormatException 停止。
Pythonmain.py · 13 行
n = int(input())total = 0missing = 0for _ in range(n): full, word = input().split() sale = None if word == "-" else int(word) # 할인가가 없으면 None if sale is None: missing += 1 total += int(full) else: total += saleprint(total)print(missing)
JavaScriptmain.js · 12 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let total = 0;let missing = 0;for (let i = 1; i <= n; i++) { const [full, word] = lines[i].trim().split(" "); const sale = word === "-" ? null : Number(word); // 할인가가 없으면 null if (sale === null) missing++; total += sale ?? Number(full); // ??는 null일 때만 오른쪽 값을 쓴다(0은 그대로 0)}console.log(total);console.log(missing);
TypeScriptmain.ts · 13 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let total = 0;let missing = 0;for (let i = 1; i <= n; i++) { const [full, word] = lines[i].trim().split(" "); const sale: number | null = word === "-" ? null : Number(word); // 할인가가 없으면 null if (sale === null) missing++; total += sale ?? Number(full); // ??는 null일 때만 오른쪽 값을 쓴다(0은 그대로 0)}console.log(total);console.log(missing);
JavaMain.java · 26 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); int total = 0; int missing = 0; for (int i = 0; i < n; i++) { String[] parts = in.readLine().trim().split(" "); int full = Integer.parseInt(parts[0]); // int는 null을 담을 수 없어 Integer를 쓴다 Integer sale = parts[1].equals("-") ? null : Integer.valueOf(parts[1]); if (sale == null) { missing++; total += full; } else { total += sale; } } System.out.println(total); System.out.println(missing); }}
C#Main.cs · 21 行
using System;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int total = 0; int missing = 0; for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); int full = int.Parse(parts[0]); int? sale = parts[1] == "-" ? null : int.Parse(parts[1]); // int? 는 null도 담는 int if (sale == null) missing++; total += sale ?? full; // ??는 null일 때만 오른쪽 값을 쓴다 } Console.WriteLine(total); Console.WriteLine(missing); }}
C++main.cpp · 20 行
#include <iostream>#include <optional>#include <string>int main() { int n; std::cin >> n; int total = 0; int missing = 0; for (int i = 0; i < n; i++) { int full; std::string word; std::cin >> full >> word; std::optional<int> sale; // 처음에는 값이 없음 if (word != "-") sale = std::stoi(word); if (!sale) missing++; total += sale.value_or(full); // 값이 없을 때만 full을 쓴다 } std::cout << total << '\n' << missing << '\n';}
Cmain.c · 26 行
#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void) { int n; if (scanf("%d", &n) != 1) return 0; int total = 0; int missing = 0; for (int i = 0; i < n; i++) { int full; char word[16]; if (scanf("%d %15s", &full, word) != 2) return 0; // C의 int에는 '없음'이 없어서 참·거짓 하나를 따로 둔다 bool has_sale = strcmp(word, "-") != 0; if (has_sale) { total += atoi(word); } else { missing++; total += full; } } printf("%d\n%d\n", total, missing); return 0;}
Craykonmain.cray · 24 行
n = 控制台.读行().转数字();total = 0;missing = 0;循环 (i = 0; i < n; i++){ parts = 控制台.读行().去空白().分割(" "); full = parts[0].转数字(); sale = 空; // 할인가가 없으면 빈값 如果 (parts[1] != "-") { sale = parts[1].转数字(); } 如果 (sale == 空) { missing++; total += full; } 否则 { total += sale; }}控制台.输出(total);控制台.输出(missing);