knyazer

Untitled

Nov 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner in = new Scanner(System.in);
  9.  
  10.         int N = in.nextInt();
  11.  
  12.         if (N == 2 || N <= 0) {
  13.             System.out.println("Not exist");
  14.             return;
  15.         }
  16.  
  17.         if (N % 2 == 0) {
  18.             System.out.println("Exist, but very hard to calculate");
  19.             return;
  20.         }
  21.  
  22.         int L = N * 2 - 1;
  23.         int step = (N - 1) / 2;
  24.         int matrix[][] = new int[L][L];
  25.  
  26.         for (int i = 0; i < L; i++) {
  27.             for (int j = 0; j < L; j++) {
  28.                 matrix[i][j] = 0;
  29.             }
  30.         }
  31.  
  32.         for (int i = 0, counter = 1; i < N; i++) {
  33.             for (int j = 0, dx = 0; j < N; j++, dx++) {
  34.                 matrix[i + dx][j + N - 1 - i] = counter;
  35.                 counter += 1;
  36.             }
  37.         }
  38.  
  39.  
  40.         for (int i = 0; i < L; i++) {
  41.             for (int j = 0; j < step; j++) {
  42.                 if (matrix[i][j] != 0)
  43.                     matrix[i][N + j] = matrix[i][j];
  44.                 if (matrix[j][i] != 0)
  45.                     matrix[N + j][i] = matrix[j][i];
  46.             }
  47.             for (int j = L - 1; j > L - step - 1; j--) {
  48.                 if (matrix[i][j] != 0)
  49.                     matrix[i][j - N] = matrix[i][j];;
  50.                 if (matrix[j][i] != 0)
  51.                     matrix[j - N][i] = matrix[j][i];
  52.             }
  53.         }
  54.  
  55.         for (int i = step; i < L - step; i++) {
  56.             for (int j = step; j < L - step; j++) {
  57.                 System.out.print(matrix[i][j] + "\t");
  58.             }
  59.             System.out.println("");
  60.         }
  61.     }
  62. }
Add Comment
Please, Sign In to add comment