Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- class Main {
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- int N = in.nextInt();
- int[][] arr;
- arr = new int[N][N];
- int x;
- int y;
- int value = 1;
- for (int i = 0; i < N; i++) {
- if (i % 2 == 0) {
- x = 0;
- y = i;
- while (y >= 0) {
- arr[x][y] = value;
- value++;
- x++;
- y--;
- }
- } else {
- x = i;
- y = 0;
- while (x >= 0) {
- arr[x][y] = value;
- value++;
- x--;
- y++;
- }
- }
- }
- for (int i = 1; i < N; i++) {
- if (i % 2 == 0) {
- x = N - 1;
- y = i;
- while (y <= N - 1) {
- arr[x][y] = value;
- value++;
- x--;
- y++;
- }
- } else {
- x = i;
- y = N - 1;
- while (x <= N - 1) {
- arr[x][y] = value;
- value++;
- x++;
- y--;
- }
- }
- }
- for (int i = 0; i < N; i++){
- for(int j = 0; j < N; j++){
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement