{ }codelper
null valuesP0281

Sale price, or list price if there is none

There are n items at a flea market. The first line gives n, and each of the next n lines gives an item's list price and sale price separated by one space. An item with no sale price set has - written in place of the number. The cost of an item is its sale price if it has one, and its list price if it does not. A sale price of 0 means the item is given away for free, and it counts as having a sale price. Print the sum of the n costs on the first line and the number of items without a sale price on the second line.

Input
1 ≤ n ≤ 1,000, 1 ≤ list price ≤ 1,000,000, the sale price is - or an integer with 0 ≤ sale price ≤ list price
Output
Two lines: the sum of the costs, then the number of items without a sale price.
Write the sum as plain digits without commas
Example 1 · stdin
4
5000 3000
2000 -
1500 0
800 800
stdout
5800
1
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

"No value" is not the same thing as the number 0.
Ways to hold a missing sale price: Python None, JS null, TS number | null, Java Integer (an int cannot hold null), C# int?, C++17 std::optional<int>, Craykon null.
C's int has no "nothing", so keep a separate boolean such as bool has_sale.
Writing "another value if missing" in one step also differs: JS, TS and C# sale ?? full, C++ sale.value_or(full).
Python, Java, C and Craykon have no ??, so you branch by asking if sale is None, sale == null, sale == null.
Three traps: (1) Python sale or full and JS sale || full treat 0 as false as well as null, so a free item (sale price 0) is charged its list price.
Counting missing ones with !sale / not sale also counts the zeros.
(2) Standing in for "nothing" with 0 makes it impossible to tell from a real sale price of 0.
C atoi("-") silently gives 0.
(3) Converting without checking for - gives NaN in JS (Number("-")), which turns the whole sum into NaN, while Python int("-") stops with ValueError and Java Integer.parseInt("-") with NumberFormatException.

Pythonmain.py · 13 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();total = 0;missing = 0;for (i = 0; i < n; i++){    parts = Console.ReadLine().Trim().Split(" ");    full = parts[0].ToNumber();    sale = null; // 할인가가 없으면 빈값    if (parts[1] != "-")    {        sale = parts[1].ToNumber();    }    if (sale == null)    {        missing++;        total += full;    }    else    {        total += sale;    }}Console.WriteLine(total);Console.WriteLine(missing);