Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By Willian Luigi
- import java.util.*;
- class Caracol {
- public static void main(String[] args) {
- Scanner inp = new Scanner(System.in);
- int n = inp.nextInt();
- int[][] caracol = new int[n][n];
- int
- x = 0,
- y = 0,
- px = 0,
- py = 0
- ;
- for (int i = 0; i < n * n + 1; ++i) {
- caracol[y][x] = i;
- x += px;
- y += py;
- if (caracol[y][x] != 0) {
- if (px == 1) {
- px = 0;
- py = 1;
- x -= 1;
- y += 1;
- }
- else if (px == -1) {
- px = 0;
- py = -1;
- x += 1;
- y -= 1;
- }
- else if (py == 1) {
- px = -1;
- py = 0;
- x -= 1;
- y -= 1;
- }
- else if (py == -1) {
- px = 1;
- py = 0;
- x += 1;
- y += 1;
- }
- }
- if (x == 0 && y == 0) {
- px = 1;
- py = 0;
- }
- else if (x == 0 && y == n - 1) {
- px = 0;
- py = -1;
- }
- if (x == n - 1 && y == n - 1) {
- px = -1;
- py = 0;
- }
- else if (x == n - 1) {
- px = 0;
- py = 1;
- }
- }
- System.out.printf("\n\n\n\n");
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- System.out.printf("%d\t", caracol[i][j]);
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment