Editorial for Just Addition


Author:kahootist

Just Addition

Python

Code 1
# 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

Code 2
// 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++

Code 3
// 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

Code 4
// 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

Code 5
// 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

Code 6
/* 
 * 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

Code 7
-- 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

Code 8
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

Alternative solution:

Code 9
main :: IO ()
main = do
  [a, b] <- map (read :: String -> Int) . words <$> getLine
  print (a + b)

-- 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
-- the resulting list of integers is destructured and assigned to the variables a and b
-- (if there are fewer or more than 2 items in the list, the code will crash)
-- print (a + b): prints the sum to console
-- All of the above is then assigned to main

OCaml

Code 10
open! Core

let () =
  (* Reads a single line from standard input *)
  In_channel.input_line_exn In_channel.stdin
  (* Split line by spaces *)
  |> String.split ~on:' '
  (* Converts each word into a string and then adds them up *)
  |> List.sum (module Int) ~f:Int.of_string
  (* Convert the sum to a string *)
  |> Int.to_string
  (* Print the sum to the console *)
  |> print_endline
;;

Alternative solution:

Code 11
open! Core

let () =
  (* Assign the resulting integers to the variables a and b
     (if there are fewer or more than 2 items in the list, the code will crash) *)
  let [ a; b ] =
    (* Reads a single line from standard input *)
    In_channel.input_line_exn In_channel.stdin
    (* Split line by spaces *)
    |> String.split ~on:' '
    (* For each word in the split line, convert it to an integer *)
    |> List.map ~f:Int.of_string
  in
  (* Print a + b to the console *)
  print_endline (Int.to_string (a + b))
;;

Go

Code 12
package main

import (
    "fmt"
)

func main() {
    // Declare three integer variables
    var a, b, s int
    // Read space-separated inputs into the addresses of a and b
    fmt.Scan(&a, &b)
    // Sum a and b up
    s = a + b
    // Print the result
    fmt.Println(s)
}

Fortran

Code 13
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

Code 14
{
    
    # Retrieving the two variables with the two record specifiers
    a = $1
    b = $2
    
    # Sum them up
    summed = a + b
    
    # Print out!
    print summed

}

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.