fork(6) download
  1. /**
  2.  * Binary Addition
  3.  *
  4.  * Description
  5.  * Write a program that, given two binary numbers represented as strings, prints
  6.  * their sum in binary. The binary strings are comma separated, two per line. The
  7.  * final answer should not have any leading zeroes. In case the answer is zero,
  8.  * just print one zero i.e. 0
  9.  *
  10.  * Input
  11.  * Your program should read lines from standard input. Each line contains two binary
  12.  * strings, separated by a comma and no spaces.
  13.  *
  14.  * Output
  15.  * For each pair of binary numbers print to standard output their binary sum, one
  16.  * per line.
  17.  *
  18.  * Example
  19.  * 110011,1010 => 111101
  20.  * 11010,00101001 => 1000011
  21.  *
  22.  * Copyright © 2012-2021 HireVue
  23.  *
  24.  */
  25. import java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.util.stream.Stream;
  29. import java.nio.charset.StandardCharsets;
  30.  
  31. public class Main {
  32. /**
  33.   * Iterate through each line of input.
  34.   */
  35. public static void main(String[] args) throws IOException {
  36. InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
  37. BufferedReader in = new BufferedReader(reader);
  38. String line;
  39. while ((line = in.readLine()) != null) {
  40. try {
  41. // String[] inputs = line.split(",");
  42. // int lvalue = Integer.valueOf(inputs[0], 2);
  43. // int rvalue = Integer.valueOf(inputs[1], 2);
  44. // System.out.println(Integer.toBinaryString(lvalue+rvalue));
  45.  
  46. System.out.println(Integer.toBinaryString(Stream
  47. .of(line.split(","))
  48. .mapToInt(str -> Integer.valueOf(str, 2))
  49. .sum()));
  50. e.printStackTrace(System.err);
  51. continue;
  52. }
  53. }
  54. }
  55. }
Success #stdin #stdout 0.08s 34296KB
stdin
110011,1010
11010,00101001
stdout
111101
1000011