Advertisement
Georgi_Benchev

Untitled

Oct 13th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_3;
  2.  
  3. import java.io.*;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Task_4_ZigZag_HackedVersion_Fastest {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10.  
  11.         String line = reader.readLine();
  12.         StringTokenizer st = new StringTokenizer(line);
  13.         short rows = Short.parseShort(st.nextToken());
  14.         short cols = Short.parseShort(st.nextToken());
  15.  
  16.         reader.close();
  17.  
  18.         long sum = 0;
  19.         for (int row = 0; row < rows; row++) {
  20.             for (int col = 0; col < cols; col++) {
  21.                 if ((row + col) % 2 == 0) {
  22.                     sum += 1 + (3 * (row + col));
  23.                 }
  24.                 if ((col % 2 == 1 && row % 2 == 1 && row != rows - 1 && col != cols - 1)
  25.                         || (col % 2 == 0 && row % 2 == 0 && row != rows - 1 && row != 0 && col != 0 && col != cols - 1)) {
  26.                     sum += 1 + (3 * (row + col));
  27.                 }
  28.             }
  29.         }
  30.  
  31.         System.out.print(sum);
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement