Editorial for A + B


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: doncho

In this task, you must print the sum of N couples of numbers.

The task is pretty simple:

  1. Read N
  2. Read N lines of couples, split them by a whitespace
  3. Print the sum of each couple

JavaScript Solution

const n = +gets();

for(let i = 0; i < n; i += 1){
    const [a, b] = gets().split(' ').map(Number);
    print(a + b);
}

quit(0);

C# Solution

using System;
using System.Linq;

public class APlusBSolution
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        for(var i = 0; i < n; i++)
        {
            var arr = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse)
                .ToArray();
            Console.WriteLine(arr[0] + arr[1]);
        }
    }
}

Java Solution

import java.io.*;

public class APlusBSolution {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        for(int i = 0; i < n; i ++) {
            String[] tokens = in.readLine().split(" ");
            int a = Integer.parseInt(tokens[0]);
            int b = Integer.parseInt(tokens[1]);
            System.out.println(a + b);
        }
    }
}

Python Solution

import sys

n = int(sys.stdin.readline())
for _ in range(n):
    (a, b) = map(int, sys.stdin.readline().split(' '))
    print(a + b)

C++ Solution

#include<iostream>

int main()
{
    int n;
    std::cin >> n;

    for(auto i = 0; i < n; ++i) {
        int a, b;
        std::cin >> a >> b;
        std::cout << a + b << '\n';
    }

    return 0;
}

Swift Solution

let n = Int(readLine()!)!

for _ in 0..<n {
    let ab = readLine()!.split {$0 == " "}
        .map { Int($0)! }
    print(ab[0] + ab[1])
}

Haskell Solution

main = do
    input <- getContents
    mapM_ (print . solve) $ tail $ lines input

solve line =
    let [x, y] = map read $ words line
    in x + y

Kotlin Solution

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()

    for (i in 0 until n) {
        val (x, y) = readLine()!!.split(' ')
                .map { it.toInt() }
        println(x + y)
    }
}

Lua Solution

n = io.read()

function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

input = {}
for i = 1, n, 1 do
    s = split(io.read(), " ")
print(math.floor(s[1] + s[2]))
end

OCaml Solution

let n = int_of_string (read_line ()) in
for i = 1 to n do
    let str = read_line () in
    let space_index = String.index_from str 0 ' ' in
    let first = String.sub str 0 space_index in
    let second = String.sub str (space_index + 1) (String.length str - 1 - space_index) in
    int_of_string first + int_of_string second |> print_int;
    print_newline ()
done

Comments

There are no comments at the moment.