Mishakis

ZigZag

Dec 3rd, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ZigZag {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int rows = scanner.nextInt();
  7.         int cols = scanner.nextInt();
  8.  
  9.         long sum = 0;
  10.         for (int i = 0; i < rows; i++) {
  11.             if (i % 2 == 0) {
  12.                 for (int j = 0; j < cols; j += 2) {
  13.                     sum += ((j * 3) + (i * 3) + 1);
  14.                     if (i != 0 && i != rows - 1 && j != 0 && j != cols - 1) {
  15.                         sum += ((j * 3) + (i * 3) + 1);
  16.                     }
  17.                 }
  18.             } else {
  19.                 for (int j = 1; j < cols; j += 2) {
  20.                     sum += ((j * 3) + (i * 3) + 1);
  21.                     if (i != 0 && i != rows - 1 && j != 0 && j != cols - 1) {
  22.                         sum += ((j * 3) + (i * 3) + 1);
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.  
  28.         System.out.println(sum);
  29.  
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment