Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package LinearDataStructures.Exercises;
- import java.util.Scanner;
- public class a_FillTheMatrix {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String[] tokens = console.nextLine().split(", ");
- int rowsCols = Integer.parseInt(tokens[0]);
- String pattern = tokens[1];
- if (pattern.equals("A")) {
- patternA(rowsCols);
- } else if (pattern.equals("B")) {
- patternB(rowsCols);
- }
- }
- private static void patternA(int rowsCols) {
- int[][] ints = new int[rowsCols][rowsCols];
- int row = 0;
- int count = 1;
- for (int i = 0; i < rowsCols; i++) {
- for (int j = 0; j < rowsCols; j++) {
- ints[j][row] = count;
- count++;
- }
- row++;
- }
- for (int[] anInt : ints) {
- for (int i : anInt) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- }
- private static void patternB(int rowsCols) {
- int[][] ints = new int[rowsCols][rowsCols];
- int row = 0;
- int count = 1;
- for (int i = 0; i < rowsCols; i++) {
- if (row % 2 == 0) {
- for (int j = 0; j < rowsCols; j++) {
- ints[j][row] = count;
- count++;
- }
- } else {
- for (int j = rowsCols-1; j >= 0; j--) {
- ints[j][row] = count;
- count++;
- }
- }
- row++;
- }
- for (int[] anInt : ints) {
- for (int i : anInt) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- }
- }
Add Comment
Please, Sign In to add comment