{ }codelper
空值 nullP0281

没有折扣价就按原价

跳蚤市场上有 n 件物品。
第一行给出 n,接下来 n 行每行给出一件物品的原价和折扣价,用一个空格隔开。
没有定折扣价的物品,在折扣价的位置写的不是数而是 - 。
物品的价钱:有折扣价就是折扣价,没有折扣价就是原价。
折扣价 0 表示白送,算作有折扣价。
第一行输出 n 件物品价钱的总和,第二行输出没有折扣价的物品数量。

输入
1 ≤ n ≤ 1,000,1 ≤ 原价 ≤ 1,000,000,折扣价是 - 或满足 0 ≤ 折扣价 ≤ 原价 的整数
输出
两行:价钱的总和、没有折扣价的物品数量。
总和只写数字,不加逗号
样例 1 · stdin
4
5000 3000
2000 -
1500 0
800 800
stdout
5800
1
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

“没有值”和数字 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);