Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class matrix1_Exercises_1Fill_The_Matrix {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(", ");
- int row = Integer.parseInt(input[0]);
- int[][] matrix = new int[row][row];
- int count = 0;
- int r = 0;
- int c = 0;
- boolean flag = true;
- if (input[1].equals("A")) {
- while (count++ < row * row) {
- matrix[r][c] = count;
- if (matrix[r][c] == row * row) {
- break;
- }
- if (r == row - 1) {
- c++;
- r = -1;
- }
- r++;
- }
- } else if (input[1].equals("B")) {
- while (count++ < row * row) {
- matrix[r][c] = count;
- if (matrix[r][c] == row * row) {
- break;
- }
- if (r == row - 1 && flag) {
- c++;
- flag = false;
- r++;
- }
- if (r == 0 && !flag) {
- c++;
- flag = true;
- r--;
- }
- if (!flag) {
- r--;
- } else {
- r++;
- }
- }
- }
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < row; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement