{ }codelper
additionP0222

Grading addition homework

A younger sibling did addition homework.
You want to grade each equation and correct the wrong answers. The first line gives the number of equations n. Each of the next n lines gives one equation the sibling wrote, in the form "a + b = c". a, b and c are integers, and there is exactly one space between each number and symbol. Negative numbers are written with a leading - (for example -8 + 3 = -5). Numbers are written with digits only, without commas, leading zeros or a + sign.
Zero is never written as -0. For each equation, print the grading result on one line. If c equals a + b, print "O a + b = c". If c differs from a + b, print "X a + b = s", where s is the correct value of a + b. Write the printed numbers with digits only, like the input. On the last line, print the number of wrong equations.

Input
1 ≤ n ≤ 1,000, -1,500,000,000 ≤ a, b ≤ 1,500,000,000, -3,000,000,000 ≤ c ≤ 3,000,000,000
Output
n lines of results, then the number of wrong equations
Example 1 · stdin
3
3 + 4 = 7
12 + 5 = 125
-8 + 3 = -11
stdout
O 3 + 4 = 7
X 12 + 5 = 17
X -8 + 3 = -5
2
Example 2 · stdin
2
1500000000 + 1500000000 = 3000000000
1500000000 + 1500000000 = -1294967296
stdout
O 1500000000 + 1500000000 = 3000000000
X 1500000000 + 1500000000 = 3000000000
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

The first trap is that one + both adds and joins strings.
In JS/TS the pieces from split(" ") are strings, so without Number you get "3" + "4" → "34", and the sibling's wrong answer 34 is graded as right (in TS, declaring number[] lets the type checker catch it).
Craykon joins them the same way if you add without ToNumber().
In Python "3" + "4" is also "34", but adding a string and a number raises an error.
In Java, C# and JS, once a string appears, everything after it is joined from left to right, so "= " + a + b becomes = 34.
Compute the sum first with s = a + b, or wrap it as (a + b).
The second trap is size.
a and b go up to 1.5 billion, so the sum reaches 3 billion, but int in C, C++, Java and C# only reaches about 2.1 billion.
In C# and Java the overflow silently turns negative (C# only reports it inside checked), and signed overflow in C and C++ is undefined behavior.
So read with C long long and scanf("%lld + %lld = %lld"), C++ long long, Java/C# long.
Python integers have no size limit, and numbers in JS, TS and Craykon are exact up to 2^53, so 3 billion is fine.

Pythonmain.py · 14 lines
n = int(input())out = []wrong = 0for _ in range(n):    a, plus, b, eq, c = input().split()    a, b, c = int(a), int(b), int(c)   # 문자열끼리 +하면 이어 붙는다("3" + "4" → "34")    s = a + b    if s == c:        out.append(f"O {a} + {b} = {s}")    else:        out.append(f"X {a} + {b} = {s}")        wrong += 1out.append(str(wrong))print("\n".join(out))
JavaScriptmain.js · 17 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];let wrong = 0;for (let i = 1; i <= n; i++) {  // 빈칸으로 나눈 조각은 문자열이라 Number로 바꿔야 +가 덧셈이 된다  const [a, , b, , c] = lines[i].trim().split(" ").map(Number);  const s = a + b;  if (s === c) {    out.push(`O ${a} + ${b} = ${s}`);  } else {    out.push(`X ${a} + ${b} = ${s}`);    wrong++;  }}out.push(String(wrong));console.log(out.join("\n"));
TypeScriptmain.ts · 18 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const out: string[] = [];let wrong = 0;for (let i = 1; i <= n; i++) {  // 빈칸으로 나눈 조각은 문자열이라 Number로 바꿔야 +가 덧셈이 된다  const [a, , b, , c]: number[] = lines[i].trim().split(" ").map(Number);  const s: number = a + b;  if (s === c) {    out.push(`O ${a} + ${b} = ${s}`);  } else {    out.push(`X ${a} + ${b} = ${s}`);    wrong++;  }}out.push(String(wrong));console.log(out.join("\n"));
JavaMain.java · 29 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());        StringBuilder out = new StringBuilder();        int wrong = 0;        for (int i = 0; i < n; i++) {            String[] p = in.readLine().trim().split(" ");            // 합이 30억까지 가니 int(약 21억)가 아니라 long으로 받는다            long a = Long.parseLong(p[0]);            long b = Long.parseLong(p[2]);            long c = Long.parseLong(p[4]);            long s = a + b;            char mark = 'O';            if (s != c) {                mark = 'X';                wrong++;            }            // 문자열 뒤에서 + a + b를 쓰면 이어 붙으니 합은 s로 따로 구해 둔다            out.append(mark).append(' ').append(a).append(" + ").append(b).append(" = ").append(s).append('\n');        }        out.append(wrong).append('\n');        System.out.print(out);    }}
C#Main.cs · 30 lines
using System;using System.Text; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        var sb = new StringBuilder();        int wrong = 0;        for (int i = 0; i < n; i++)        {            string[] p = Console.ReadLine().Trim().Split(' ');            // 합이 30억까지 가니 int가 아니라 long으로 받는다            long a = long.Parse(p[0]);            long b = long.Parse(p[2]);            long c = long.Parse(p[4]);            long s = a + b;            char mark = 'O';            if (s != c)            {                mark = 'X';                wrong++;            }            sb.Append($"{mark} {a} + {b} = {s}\n");        }        sb.Append(wrong).Append('\n');        Console.Write(sb);    }}
C++main.cpp · 20 lines
#include <iostream> int main() {    std::ios::sync_with_stdio(false);    int n, wrong = 0;    std::cin >> n;    for (int i = 0; i < n; i++) {        long long a, b, c;        char plus, eq;        std::cin >> a >> plus >> b >> eq >> c;        long long s = a + b;  // int로 더하면 30억에서 넘친다        char mark = 'O';        if (s != c) {            mark = 'X';            wrong++;        }        std::cout << mark << ' ' << a << " + " << b << " = " << s << "\n";    }    std::cout << wrong << "\n";}
Cmain.c · 20 lines
#include <stdio.h> int main(void) {    int n, wrong = 0;    long long a, b, c;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        /* 형식 문자열의 " + "와 " = "가 기호를 건너뛰어 준다 */        scanf("%lld + %lld = %lld", &a, &b, &c);        long long s = a + b; /* int로 더하면 30억에서 넘친다 */        char mark = 'O';        if (s != c) {            mark = 'X';            wrong++;        }        printf("%c %lld + %lld = %lld\n", mark, a, b, s);    }    printf("%d\n", wrong);    return 0;}
Craykonmain.cray · 19 lines
n = Console.ReadLine().ToNumber();wrong = 0;for (i = 0; i < n; i++){    p = Console.ReadLine().Trim().Split(" ");    // 나눈 조각은 문자열이라 수로변환해야 +가 덧셈이 된다    a = p[0].ToNumber();    b = p[2].ToNumber();    c = p[4].ToNumber();    s = a + b;    mark = "O";    if (s != c)    {        mark = "X";        wrong++;    }    Console.WriteLine(mark + " " + a + " + " + b + " = " + s);}Console.WriteLine(wrong);