Advertisement
deyanmalinov

01. Fill the Matrix

May 25th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args){
  5.         Scanner scan = new Scanner(System.in);
  6.         String[] line = scan.nextLine().split(", ");
  7.         int matSize = Integer.parseInt(line[0]);
  8.         char pattern = line[1].charAt(0);
  9.         int num = 1;
  10.         int row ;
  11.         int col ;
  12.  
  13.         int[][] matrix = new int[matSize][matSize];
  14.         switch (pattern){
  15.             case 'A':
  16.                 for (col = 0; col < matSize; col++) {
  17.                     for (row = 0; row < matSize; row++) {
  18.                         matrix[row][col] = num;
  19.                         num++;
  20.                     }
  21.                 }
  22.                 PrintMatrix(matrix);
  23.                 break;
  24.             case 'B':
  25.  
  26.                 for (col = 0; col < matSize; col++) {
  27.                     if (col%2==0){
  28.                         for (row = 0; row < matSize; row++) {
  29.                             matrix[row][col] = num;
  30.                             num++;
  31.                         }
  32.                     } else {
  33.                         for (row = matSize-1; row >= 0 ; row--) {
  34.                             matrix[row][col] = num;
  35.                             num++;
  36.                         }
  37.                     }
  38.                 }
  39.                 PrintMatrix(matrix);
  40.                 break;
  41.         }
  42.     }
  43.     private static void PrintMatrix (int[][] matrix){
  44.         for (int row = 0; row <matrix.length ; row++) {
  45.             for (int col = 0; col < matrix[0].length; col++) {
  46.                 System.out.print(matrix[row][col] + " ");
  47.             }
  48.             System.out.println();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement