Editorial of MindCoding 2016 Test Round (Div. 2)

10 - A+B

A+B is one of the most complex problems on this website due to its large variety of solutions.

C

    1 #include<stdio.h>
    2 
    3 int main(void) {
    4 	int a, b;
    5 	scanf("%d%d", &a, &b);
    6 	printf("%d", a + b);
    7 	return 0;
    8 }

C++

    1 #include<iostream>
    2 
    3 int main(void){
    4 	int a, b;
    5 	std::cin >> a >> b;
    6 	std::cout << a + b;
    7 	return 0;
    8 }

D

    1 import std.stdio;
    2 
    3 void main(){
    4     long a, b;
    5     readf("%d %d", &a, &b);
    6     writeln(a+b);
    7 }

Go

    1 package main
    2 import "fmt"
    3 
    4 func main() {
    5 	var a, b int
    6 	fmt.Scanf("%d%d", &a, &b)
    7 	fmt.Print(a + b)
    8 }

GolfScript
    1 ~+

Haskell

    1 import Control.Applicative
    2 
    3 main :: IO ()
    4 main = do
    5         [ a, b ] <- map read . words <$> getLine
    6         print (a + b :: Int)

Java

In Java, the class must be named prog.
    1 public class prog {
    2 	public static void main(final String[] args) {
    3 		java.util.Scanner scan = new java.util.Scanner(System.in);
    4 		System.out.println(scan.nextInt() + scan.nextInt());
    5 	}
    6 }

Oberon

    1 MODULE prog;
    2 
    3 IMPORT In, Out;
    4 
    5 VAR a, b : INTEGER;
    6 
    7 BEGIN
    8   In.Int(a);
    9   In.Int(b);
   10   Out.Int(a + b, 0);
   11   Out.Ln
   12 END prog.

OCaml

    1 let sum a b = a + b
    2 
    3 let _=
    4     Printf.printf "%i\n" ((Scanf.scanf "%i %i" sum))

Pascal

    1 var a,b:integer;
    2 begin
    3 read(a,b);
    4 write(a+b);
    5 end.

Perl

    1 #!/usr/bin/perl
    2 use v5.14;
    3 use warnings;
    4 
    5 my ($x, $y) = split ' ', <>;
    6 say $x + $y;

PHP

    1 <?php
    2 fscanf(STDIN, "%d %d\n", $a, $b);
    3 echo $a+$b . "\n";
    4 ?>

Python

    1 #!/usr/bin/python
    2 print sum(map(int, raw_input().split()))

Python3

    1 #!/usr/bin/python3
    2 print(sum(map(int, input().split())))

Ruby

    1 p gets.split(' ').map(&:to_i).inject 0, :+

Rust

    1 use std::io;
    2 
    3 fn main() {
    4     let mut input = String::new();
    5 
    6     let _ = io::stdin().read_line(&mut input);
    7 
    8     let mut sum: i64 = 0;
    9     for x in input.trim().split(" ").collect::<Vec<&str>>() {
   10         sum += match x.parse::<i64>() {
   11             Ok(val) => val,
   12             _       => 0,
   13         };
   14     }
   15 
   16     println!("{}", sum);
   17 }

SBCL

    1 (format t "~d" (+ (read) (read)))

100 - Transpose

Python 2.7

    1 n, m = map(int, raw_input().split())
    2 a = [list(raw_input().split()) for i in range(n)]
    3 
    4 print '\n'.join([' '.join([a[i][j] for i in range(n)]) for j in range(m)])

250 - Spiral

C++ (Inelus Gabriel)

    1 #include <cstdio>
    2 int N, M, A[1005][1005];
    3 int main()
    4 {
    5     scanf("%d%d", &N, &M);
    6     for(int i = 1; i <= N; ++i)
    7     {
    8         A[i][0] = A[i][M+1] = -1;
    9         for(int j = 1; j <= M; ++j)
   10             scanf("%d",&A[i][j]);
   11     }
   12 
   13     for(int j = 1; j <= M; ++j)
   14         A[0][j] = A[N+1][j] = -1;
   15 
   16     int i = 1, j = 1, ok = 1;
   17     printf("%d ",A[i][j]); A[i][j] = -1;
   18     while(ok) {
   19         ok = 0;
   20         while(A[i][j+1] != -1) { ok = 1; ++j; printf("%d ", A[i][j]); A[i][j] = -1; }
   21         while(A[i+1][j] != -1) { ok = 1; ++i; printf("%d ", A[i][j]); A[i][j] = -1; }
   22         while(A[i][j-1] != -1) { ok = 1; --j; printf("%d ", A[i][j]); A[i][j] = -1; }
   23         while(A[i-1][j] != -1) { ok = 1; --i; printf("%d ", A[i][j]); A[i][j] = -1; }
   24     }
   25     return 0;
   26 }

Python 2.7 (Sergiu Puscas)

    1 n, m = map(int, raw_input().split())
    2 a = [list(raw_input().split()) for i in range(n)]
    3 
    4 U, D, L, R = 0, n-1, 0, m-1
    5 direction = 0
    6 i, j = 0, 0
    7 
    8 for step in range(n*m):
    9     print a[i][j],
   10 
   11     if direction == 0:          # right
   12         if j+1 > R:             # change to down
   13             direction = 1
   14             R -= 1
   15             i += 1
   16         else:
   17             j += 1
   18 
   19     elif direction == 1:        # down
   20         if i+1 > D:             # change to left
   21             direction = 2
   22             D -= 1
   23             j -= 1
   24         else:
   25             i += 1
   26 
   27     elif direction == 2:        # left
   28         if j - 1 < L:           # change to up
   29             direction = 3
   30             L += 1
   31             i -= 1
   32         else:
   33             j -= 1
   34 
   35     elif direction == 3:        # up
   36         if i - 1 <= U:          # change to right
   37             direction = 0
   38             U += 1
   39             j += 1
   40         else:
   41             i -= 1
Questions?

Sponsors Gold