Editorial for A Plus 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.

Author: moncef

Read \(N\)

Repeat for each \(i\) from \(1\) to \(N\)

  • Read \(a\)
  • Read \(b\)
  • output \(a+b\)

End

Assembly Solution

#features: libc

.data
str:
    .string "%d"

str2:
    .string "%d %d"

str3:
    .string "%d\n"

.text

.globl main
main:
    pushq %rbp
    movq %rsp, %rbp
    subq $16, %rsp

    movq $str, %rdi
    leaq -4(%rbp), %rsi

    call scanf

loop:
    movq $str2, %rdi
    leaq -8(%rbp), %rsi
    leaq -12(%rbp), %rdx

    call scanf

    movl -8(%rbp), %eax
    addl -12(%rbp), %eax

    movq $str3, %rdi
    movl %eax, %esi

    call printf

    decl -4(%rbp)
    cmpl $0, -4(%rbp)

    jne loop

    movq $0, %rax

    leave

    ret

Python Solution

N = int(input())

for _ in range(N):
    a, b = map(int, input().split())
    print(a + b)

Java Solution

import java.util.*;

public class APlusB {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        for (int i = 0; i < N; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

JavaScript Solution

let args = gets();
 for (let i = 0; i < args; i++) {
     let numbers = gets().split(' ');
     print(parseInt(numbers[0]) + parseInt(numbers[1]));
 }

PHP Solution

<?php
$n = rtrim(fgets(STDIN));
$result = [];
for ($i=0; $i < $n; $i++){
  $add = rtrim(fgets(STDIN));
  $result[] += array_sum(explode(" ", $add));
}
for($i=0; $i < count($result); $i++){
  echo "$result[$i] \n";
} ?>

C# Solution

using System;

namespace aplusb
{
    class Program
    {
        static void Main(string[] args)
        {
            var count = int.Parse(Console.ReadLine());

            for(var x = 0; x < count; x++) {
                var parts = Console.ReadLine().Split(' ');
                System.Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1]));
            }
        }
    }
}

C++ Solution

#include <iostream>

using namespace std;

int main() {
    int N;
    cin >> N;

    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        cout << a + b << endl;
    }
}

Perl Solution

chomp($n = <>);
for ($i = 0;$i<$n;$i = $i +1)
{
    chomp($line = <>);
    ($a,$b)= split / +/, $line;
    $c = $a + $b;
    print "$c\n";
}

Ruby Solution

n = gets.to_i
n.times { 
    a, b = *gets.split.map{|x|x.to_i}
    puts a+b
}

Comments