Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.util.Scanner;
- public class FillTheMatrix {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String[] inputs = input.split(" ");
- int N = Integer.parseInt(inputs[0].replace(",", ""));
- char patternType = inputs[1].charAt(0);
- int[][] matrix = new int[N][N];
- if (patternType == 'A') {
- fillPatternA(matrix, N);
- } else if (patternType == 'B') {
- fillPatternB(matrix, N);
- } else {
- System.out.println("Invalid pattern type. Please enter 'A' or 'B'.");
- return;
- }
- printMatrix(matrix, N);
- }
- // Method to fill the matrix with Pattern A
- public static void fillPatternA(int[][] matrix, int N) {
- int num = 1;
- for (int col = 0; col < N; col++) {
- for (int row = 0; row < N; row++) {
- matrix[row][col] = num++;
- }
- }
- }
- // Method to fill the matrix with Pattern B
- public static void fillPatternB(int[][] matrix, int N) {
- int num = 1;
- for (int col = 0; col < N; col++) {
- if (col % 2 == 0) {
- for (int row = 0; row < N; row++) {
- matrix[row][col] = num++;
- }
- } else {
- for (int row = N - 1; row >= 0; row--) {
- matrix[row][col] = num++;
- }
- }
- }
- }
- // Method to print the matrix
- public static void printMatrix(int[][] matrix, int N) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement