Advertisement
Georgi_Benchev

Untitled

Oct 12th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_3;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task5_cleenStart {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = scanner.nextInt();
  10.         int[][] matrix = new int[n][n];
  11.  
  12.         int rounds = 0;
  13.         int direction = 0;
  14.         int rounds2 = 0;
  15.         int newCount = 0;
  16.  
  17.  
  18.         for (int count = 1; count <= n * n; count++) {
  19.  
  20.  
  21.             if (direction == 0) {
  22.  
  23.                 matrix[rounds][rounds + newCount] = count;
  24.  
  25.  
  26.             } else if (direction == 1) {
  27.  
  28.                 matrix[rounds + newCount][n - 1 - rounds] = count;
  29.  
  30.  
  31.             } else if (direction == 2) {
  32.  
  33.                 matrix[n - 1 - rounds][n - 1 - rounds - newCount] = count;
  34.  
  35.             } else if (direction == 3) {
  36.  
  37.                 matrix[n - 1 - rounds - newCount][rounds] = count;
  38.  
  39.             }
  40.  
  41.             newCount++;
  42.             if (n-1-rounds2==newCount) {
  43.                 direction++;
  44.                 newCount = 0;
  45.             }
  46.             if (direction == 4) {
  47.                 rounds2+=2;
  48.                 rounds++;
  49.                 direction = 0;
  50.             }
  51.  
  52.  
  53.         }
  54.  
  55.         for (int[] array : matrix) {
  56.             for (int digit : array) {
  57.                 System.out.print(digit + " ");
  58.             }
  59.             System.out.println();
  60.         }
  61.  
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement