Obsahujem riešenia obidvoch úvodných problémov na Hackerrank – Solve me first, Solve me second. Stačí si len vybrať programovací jazyk a odovzdať riešenie, všetko ostané je pre nás vytvorené.
Obidve riešenia:
Solve me first
Scala
1 2 3 4 5 |
object SolveMeFirst { def main(args: Array[String]) { println(io.Source.stdin.getLines().take(2).map(_.toInt).sum) } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class SolveMeFirst { static int solveMeFirst(int a, int b) { return a+b; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int a; a = in.nextInt(); int b; b = in.nextInt(); int sum; sum = solveMeFirst(a, b); System.out.println(sum); } } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function solveMeFirst(a, b) { return a+b; } function main() { // write your code here. // call `readLine()` to read a line. // use console.log() to write to stdout var a = parseInt(readLine()); var b = parseInt(readLine());; var res = solveMeFirst(a, b); console.log(res); } |
Ruby
1 2 3 4 5 6 7 8 |
def solveMeFirst (a, b) return a+b end val1 = gets.to_i val2 = gets.to_i sum = solveMeFirst(val1,val2) print (sum) |
Solve me second
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 |
object SolveMeSecond extends App { val n = readInt //recommended (1 to n).map(i => readLine.split(" ").map(_.toInt).sum).foreach(println) /* another way for (i <- 1 to n) { val Array(a, b) = readLine.split(" ").map(_.toInt) println(a + b) } */ } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; class SolveMeSecond { static int solveMeSecond(int a, int b) { return a+b; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t; int sum; int a,b; t = in.nextInt(); for (int i=0;i<t;i++) { a = in.nextInt(); b = in.nextInt(); sum = solveMeSecond(a, b); System.out.println(sum); } } } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
process.stdin.resume(); process.stdin.setEncoding('ascii'); var __input_stdin = ""; var __input_stdin_array = ""; var __input_currentline = 0; process.stdin.on('data', function (data) { __input_stdin += data; }); process.stdin.on('end', function () { __input_stdin_array = __input_stdin.split("\n"); var res; var n = parseInt(__input_stdin_array[__input_currentline].trim(), 10); __input_currentline += 1; for (var i = 0; i<n;i++) { var _line = __input_stdin_array[__input_currentline].trim(); __input_currentline += 1; var line = _line.split(" "); var _a = parseInt(line[0]); var _b = parseInt(line[1]); res = _a + _b; process.stdout.write(""+res+"\n"); } }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 |
def solveMeSecond (a, b) return a+b end t = gets.to_i (0...t).each do |i| lis = gets.strip.split(" ") a = lis[0].to_i b = lis[1].to_i res = solveMeSecond(a,b) puts res end |