emodev

Untitled

Nov 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package LinearDataStructures.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class a_FillTheMatrix {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.         String[] tokens = console.nextLine().split(", ");
  9.         int rowsCols = Integer.parseInt(tokens[0]);
  10.         String pattern = tokens[1];
  11.         if (pattern.equals("A")) {
  12.             patternA(rowsCols);
  13.         } else if (pattern.equals("B")) {
  14.             patternB(rowsCols);
  15.         }
  16.  
  17.     }
  18.  
  19.     private static void patternA(int rowsCols) {
  20.         int[][] ints = new int[rowsCols][rowsCols];
  21.         int row = 0;
  22.         int count = 1;
  23.         for (int i = 0; i < rowsCols; i++) {
  24.  
  25.             for (int j = 0; j < rowsCols; j++) {
  26.                 ints[j][row] = count;
  27.                 count++;
  28.             }
  29.             row++;
  30.  
  31.         }
  32.         for (int[] anInt : ints) {
  33.             for (int i : anInt) {
  34.                 System.out.print(i + " ");
  35.             }
  36.             System.out.println();
  37.         }
  38.     }
  39.  
  40.     private static void patternB(int rowsCols) {
  41.         int[][] ints = new int[rowsCols][rowsCols];
  42.         int row = 0;
  43.         int count = 1;
  44.  
  45.         for (int i = 0; i < rowsCols; i++) {
  46.  
  47.             if (row % 2 == 0) {
  48.                 for (int j = 0; j < rowsCols; j++) {
  49.                     ints[j][row] = count;
  50.                     count++;
  51.                 }
  52.             } else {
  53.                 for (int j = rowsCols-1; j >= 0; j--) {
  54.                     ints[j][row] = count;
  55.                     count++;
  56.                 }
  57.             }
  58.             row++;
  59.         }
  60.         for (int[] anInt : ints) {
  61.             for (int i : anInt) {
  62.                 System.out.print(i + " ");
  63.             }
  64.             System.out.println();
  65.         }
  66.  
  67.     }
  68. }
Add Comment
Please, Sign In to add comment