Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class VankatasTask {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner Console = new Scanner(System.in);
  8.  
  9.         int row = Console.nextInt();
  10.         int col = Console.nextInt();
  11.  
  12.         int[][] matrix = new int[row][col];
  13.         int startNum = 1;
  14.         int endNum = row*col;
  15.  
  16.         int currentRow = 0;
  17.         int currentCol = 0;
  18.  
  19.         String direction = "up";
  20.  
  21.         while (startNum <= endNum) {
  22.  
  23.             if (startNum == 1) {
  24.  
  25.                 matrix[currentRow][currentCol] = startNum;
  26.                 startNum++;
  27.  
  28.             } else {
  29.  
  30.                 if (direction.equals("up")) {
  31.  
  32.                     currentRow--;
  33.                     currentCol++;
  34.  
  35.                     if (currentRow > -1) {
  36.  
  37.                         if (currentCol == col) {
  38.  
  39.                             currentCol--;
  40.                             currentRow+=2;
  41.                             matrix[currentRow][currentCol] = startNum;
  42.                             startNum++;
  43.                             direction = "down";
  44.  
  45.                         } else {
  46.  
  47.                             matrix[currentRow][currentCol] = startNum;
  48.                             startNum++;
  49.  
  50.                         }
  51.  
  52.                     } else {
  53.  
  54.                         currentRow++;
  55.  
  56.                         if (currentCol == col) {
  57.  
  58.                             currentRow++;
  59.                             currentCol--;
  60.                             matrix[currentRow][currentCol] = startNum;
  61.                             startNum++;
  62.                             direction = "down";
  63.  
  64.                         } else {
  65.  
  66.                             matrix[currentRow][currentCol] = startNum;
  67.                             startNum++;
  68.                             direction = "down";
  69.  
  70.                         }
  71.                     }
  72.  
  73.                 } else { // direction is down
  74.  
  75.                     currentRow++;
  76.                     currentCol--;
  77.  
  78.                     if (currentCol > -1) {
  79.  
  80.                         if (currentRow < row) {
  81.  
  82.                             matrix[currentRow][currentCol] = startNum;
  83.                             startNum++;
  84.  
  85.                         } else {
  86.  
  87.                             currentRow--;
  88.                             currentCol+=2;
  89.                             matrix[currentRow][currentCol] = startNum;
  90.                             startNum++;
  91.                             direction = "up";
  92.  
  93.                         }
  94.  
  95.                     } else{
  96.  
  97.                         if (currentRow == row) {
  98.  
  99.                             currentRow--;
  100.                             currentCol+=2;
  101.                             matrix[currentRow][currentCol] = startNum;
  102.                             direction = "up";
  103.                             startNum++;
  104.  
  105.                         } else {
  106.  
  107.                             currentCol++;
  108.                             matrix[currentRow][currentCol] = startNum;
  109.                             direction = "up";
  110.                             startNum++;
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.  
  117.         for (int i = 0; i < row; i++) {
  118.             for (int j = 0; j < col; j++) {
  119.                 System.out.print(matrix[i][j] + " ");
  120.             }
  121.             System.out.println();
  122.         }
  123.  
  124.  
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement