Editorial for Just Addition


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: kahootist

Just Addition

Python

# Get the first line of input and store it
input_line = input()

# Split the line into two parts 
split_input = input_line.split()

# Retrieve the two integer inputs a and b,
# by getting the first and second items in split_input,
# then converting them into integers.
a = int(split_input[0])
b = int(split_input[1])

# Add the two integer inputs up,
# and printing the final result.
answer = a + b
print(answer)

Java

// We import the Scanner module to read user inputs...
import java.util.Scanner;

// We write the public main function inside the public Main class to execute the code.
public class Main {
    public static void main(String[] args) {

        // A new scanner object is created
        Scanner scanner = new Scanner(System.in);

        // We use the scanner.nextInt() method to retrieve the two integers in the given order...
        // Note that the constraints specify that the two integers will not exceed 10^9 in value,
        // which is within the 32-bit integer limit.
        int a = scanner.nextInt();
        int b = scanner.nextInt();

        // Output their sum using the System.out.println() method.
        System.out.println(a + b);

    }
}

C++

// This line includes all the standard libraries in C++.
#include <bits/stdc++.h>

// This line defines the namespace std.
using namespace std;

// Main function!
int main() {

    // Declare two integer variables of a and b.
    int a, b;

    // Retrieve their value through cin.
    cin >> a >> b;

    // Calculate their sum.
    int sum = a + b;

    // Output the sum through to cout.
    cout << sum << endl;

}

C Sharp

// Using the System library!
using System;

// Main function in the Program class...
class Program {
    static void Main() {

        // Read the input line
        string line = Console.ReadLine();

        // Split the line into an array of strings
        string[] parts = line.Split(' ');

        // Convert the parts to integers
        int a = int.Parse(parts[0]);
        int b = int.Parse(parts[1]);

        // Calculate the sum
        int summed = a + b;

        // Print the sum!
        Console.WriteLine(summed);

    }
}

C

// Include the Standard IO to handle the input/output
#include <stdio.h>

// Main function
int main() {

    // Declare two integer variables of a and b
    int a, b;

    // Scan two space-separated integers,
    // and store them at the locations of a and b
    scanf("%d %d", &a, &b);

    // Calculate the sum
    int summed = a + b;

    // Print the result!
    printf("%d", summed);

    // Return
    return 0;

}

JavaScript

/* 
 * This is a custom version of V8 that adds six functions in order to perform I/O and aid in online judging.
 *
 * * `print(...)`: similar to Python's `print`, prints all argument separated by space followed by new line.
 * * `flush()`: flushes stdout, ensuring everything output by `print()` immediately shows up.
 * * `gets()`: similar to the Ruby equivalent, returns one line of input from `stdin`.
 * * `read(bytes)`: read `bytes` bytes from stdin as an `ArrayBuffer`.
 * * `write(buffer)`: write a typed array, `ArrayBuffer`, or a view of `ArrayBuffer` to stdout.
 * * `quit(code)`: exits the program with `code`.
 * * You can also assign to the global variable `autoflush` to control whether `print()` flushes.
 *
 */

// Read two numbers from input via `gets()`,
// splits them by space, then map them to numbers
const [a, b] = gets().split(' ').map(Number);

// Calculate the sum of the two numbers
const sum = a + b;

// Output the sum!
print(a + b)

Lua

-- Read in the line of input from standard input
line = io.read()

-- Use pattern matching to assign the variables a and b from line
a, b = line:match("(%d+)%s(%d+)")

-- Convert the strings to numbers and sum them
a = tonumber(a)
b = tonumber(b)
summed = a + b

-- Print the result!
print(summed)

Haskell

main :: IO ()
main = print . sum . map (read :: String -> Int) . words =<< getLine

-- getLine reads a single line from the standard input;
-- then, in order, the composed function does the following...
-- - words: splits the previous string input into a list of strings by whitespace
-- - map (read :: String -> Int): maps the read function (which converts a String to an Int, as noted in the signature) to everything in the previous list
-- - sum: sums everything up in the previous integer list
-- - print: prints to the console
-- All of the above is then assigned to main

Fortran

program just_addition

    ! Mention to explicitly type every variable in this program
    implicit none

    ! Three variables are declared upfront: a, b, and summed
    integer :: a, b, summed

    ! Read in two integers from standard input
    read *, a, b

    ! Add the two numbers up and store it in summed
    summed = a + b

    ! Print out the final result!
    print *, summed

end program just_addition

AWK

{

    # Retrieving the two variables with the two record specifiers
    a = $1
    b = $2

    # Sum them up
    summed = a + b

    # Print out!
    print summed

}

Comments

There are no comments at the moment.