Advertisement
enevlogiev

Lego Bricks

Feb 9th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Problem03 {
  5.     @SuppressWarnings("resource")
  6.     public static void main(String[] args) {
  7.         Scanner scn = new Scanner(System.in);
  8.         int numberOfRows = Integer.parseInt(scn.nextLine().trim());
  9.         String[][] firstJagged = new String[numberOfRows][];
  10.         String[][] secondJagged = new String[numberOfRows][];
  11.        
  12.         for (int i = 0; i < numberOfRows; i++) {
  13.             firstJagged[i] = scn.nextLine().trim().split("\\s+");
  14.         }
  15.         for (int i = 0; i < numberOfRows; i++) {
  16.             secondJagged[i] = scn.nextLine().trim().split("\\s+");
  17.         }
  18.         //check
  19.         boolean matching = true;
  20.         int combinedLength = firstJagged[0].length + secondJagged[0].length;
  21.         for (int i = 1; i < numberOfRows; i++) {
  22.             int nextLength = firstJagged[i].length + secondJagged[i].length;
  23.             if (nextLength != combinedLength) {
  24.                 matching = false;
  25.                 break;
  26.             }
  27.         }
  28.         //print accordingly
  29.         if (matching) {
  30.             for (int i = 0; i < numberOfRows; i++) {
  31.                 String output = "";
  32.                 for (int j = 0; j < firstJagged[i].length; j++) {
  33.                     output += firstJagged[i][j] + ", ";
  34.                 }
  35.                 for (int j = secondJagged[i].length - 1; j >= 0; j--) {
  36.                     output += secondJagged[i][j] + ", ";
  37.                 }
  38.                 System.out.println("[" + output.substring(0, output.length() - 2) + "]");
  39.             }
  40.         }
  41.         else {
  42.             int totalLength = 0;
  43.             for (int i = 0; i < numberOfRows; i++) {
  44.                 totalLength += firstJagged[i].length + secondJagged[i].length;
  45.             }
  46.             System.out.println("The total number of cells is: " + totalLength);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement