Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class VankatasTask {
- public static void main(String[] args) {
- Scanner Console = new Scanner(System.in);
- int row = Console.nextInt();
- int col = Console.nextInt();
- int[][] matrix = new int[row][col];
- int startNum = 1;
- int endNum = row*col;
- int currentRow = 0;
- int currentCol = 0;
- String direction = "up";
- while (startNum <= endNum) {
- if (startNum == 1) {
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- } else {
- if (direction.equals("up")) {
- currentRow--;
- currentCol++;
- if (currentRow > -1) {
- if (currentCol == col) {
- currentCol--;
- currentRow+=2;
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- direction = "down";
- } else {
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- }
- } else {
- currentRow++;
- if (currentCol == col) {
- currentRow++;
- currentCol--;
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- direction = "down";
- } else {
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- direction = "down";
- }
- }
- } else { // direction is down
- currentRow++;
- currentCol--;
- if (currentCol > -1) {
- if (currentRow < row) {
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- } else {
- currentRow--;
- currentCol+=2;
- matrix[currentRow][currentCol] = startNum;
- startNum++;
- direction = "up";
- }
- } else{
- if (currentRow == row) {
- currentRow--;
- currentCol+=2;
- matrix[currentRow][currentCol] = startNum;
- direction = "up";
- startNum++;
- } else {
- currentCol++;
- matrix[currentRow][currentCol] = startNum;
- direction = "up";
- startNum++;
- }
- }
- }
- }
- }
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement