Advertisement
Guest User

snake

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