svetlozar_kirkov

Lego Blocks

May 8th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.         boolean equal = true;
  10.         int rows = Integer.parseInt(input.nextLine());
  11.         String[][] firstArray = new String[rows][];
  12.         String[][] secondArray = new String[rows][];
  13.         int totalLength = 0;
  14.         for (int i = 0; i < rows; i++) {
  15.             firstArray[i] = input.nextLine().trim().split("\\s+");
  16.             totalLength += firstArray[i].length;
  17.         }
  18.         for (int j = 0; j < rows; j++) {
  19.             secondArray[j] = input.nextLine().trim().split("\\s+");
  20.             totalLength += secondArray[j].length;
  21.         }
  22.         int template = firstArray[0].length + secondArray[0].length;
  23.         for (int k = 0; k < rows; k++) {
  24.             if (firstArray[k].length + secondArray[k].length != template) {
  25.                 equal = false;
  26.             }
  27.         }
  28.         if (equal == false) {
  29.             System.out.println("The total number of cells is: " + totalLength);
  30.         }
  31.         else {
  32.             for (int m = 0; m < rows; m++) {
  33.                 System.out.print("[");
  34.                 System.out.print(combine(firstArray[m], ", "));
  35.                 System.out.print(", ");
  36.                 Collections.reverse(Arrays.asList(secondArray[m]));
  37.                 System.out.print(combine(secondArray[m], ", "));
  38.                 System.out.println("]");
  39.             }
  40.         }
  41.     }
  42.     public static String combine(String[] s, String glue)
  43.     {
  44.         int k = s.length;
  45.         if ( k == 0 )
  46.         {
  47.             return null;
  48.         }
  49.         StringBuilder out = new StringBuilder();
  50.         out.append( s[0] );
  51.         for ( int x=1; x < k; ++x )
  52.         {
  53.             out.append(glue).append(s[x]);
  54.         }
  55.         return out.toString();
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment