Advertisement
Georgi_Benchev

Untitled

Oct 12th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_3;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task4_ZigZag {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int rows = scanner.nextInt();
  10.         int cols = scanner.nextInt();
  11.  
  12.         long sum = 0;
  13.         boolean check = true;
  14.         for (int row = 0; row < rows; row++) {
  15.             if (check && row % 2 == 0) {
  16.                 for (int col = 0; col < cols; col++) {
  17.                     sum += 1 + (3L * (row + col));
  18.                     if (rows % 2 == 0 && col == cols - 1 && row == rows - 1) {
  19.                         check = false;
  20.                         break;
  21.                     } else if (rows % 2 == 1 && col == 0 && row == rows - 1) {
  22.                         check = false;
  23.                         break;
  24.                     }
  25.                     if (col % 2 == 0) {
  26.                         row++;
  27.                     } else {
  28.                         row--;
  29.                     }
  30.                 }
  31.             } else if (check) {
  32.                 for (int col = cols - 2; col >= 0; col--) {
  33.                     if (rows % 2 == 0 && col == cols - 1 && row == rows - 1) {
  34.                         check = false;
  35.                         break;
  36.                     } else if (rows % 2 == 1 && col == 0 && row == rows - 1) {
  37.                         check = false;
  38.                         break;
  39.                     }
  40.                     if (col % 2 == 0 && col != 0) {
  41.                         row++;
  42.                         sum += 1 + (3L * (row + col));
  43.                     } else if (col != 0) {
  44.                         row--;
  45.                         sum += 1 + (3L * (row + col));
  46.                     }
  47.                 }
  48.             } else {
  49.                 break;
  50.             }
  51.         }
  52.         System.out.println(sum);
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement