Last active
May 29, 2018 14:03
-
-
Save KavyrshinR/82945936183e724047bf75730a0459f2 to your computer and use it in GitHub Desktop.
SpiralCicle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class SpiralCicle { | |
| private static int n = 0; | |
| private static int[][] array; | |
| public static void main(String [] args) { | |
| Scanner in = new Scanner(System.in); | |
| n = in.nextInt(); | |
| if (n < 1 || n > Integer.MAX_VALUE / 2) { | |
| System.out.println("Wrong argument"); | |
| } else { | |
| int size = 2 * n - 1; | |
| array = new int[size][size]; | |
| for (int i = 0; i < array.length; i++) { | |
| for (int j = 0; j < array[i].length; j++) { | |
| array[i][j] = (int) (Math.random() * 9); | |
| } | |
| } | |
| System.out.println(""); | |
| for (int i = 0; i < array.length; i++) { | |
| for (int j = 0; j < array[i].length; j++) { | |
| System.out.print(array[i][j] + ", "); | |
| } | |
| System.out.println(""); | |
| } | |
| System.out.println(""); | |
| printArray(); | |
| } | |
| } | |
| public static void printArray() { | |
| int arrayCenter = n - 1; | |
| int i = arrayCenter; | |
| int j = arrayCenter; | |
| System.out.print(array[i][j] + " "); | |
| while ((i != 0) || (j != 0)) { | |
| if (i > j) { | |
| if ((double) (i + j) / arrayCenter == 2.0) { | |
| j++; | |
| } else if (i + j < 2 * arrayCenter) { | |
| i++; | |
| } else if (i + j > 2 * arrayCenter) { | |
| j++; | |
| } | |
| } else if (i < j) { | |
| if ((double) (i + j) / arrayCenter == 2.0) { | |
| j--; | |
| } else if (i + j > 2 * arrayCenter) { | |
| i--; | |
| } else if (i + j < 2 * arrayCenter) { | |
| j--; | |
| } | |
| } else if (i == j) { | |
| if (i <= arrayCenter) { | |
| j--; | |
| } else { | |
| i--; | |
| } | |
| } | |
| System.out.print(array[i][j] + " "); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment