Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ZigZag {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int rows = scanner.nextInt();
- int cols = scanner.nextInt();
- long sum = 0;
- for (int i = 0; i < rows; i++) {
- if (i % 2 == 0) {
- for (int j = 0; j < cols; j += 2) {
- sum += ((j * 3) + (i * 3) + 1);
- if (i != 0 && i != rows - 1 && j != 0 && j != cols - 1) {
- sum += ((j * 3) + (i * 3) + 1);
- }
- }
- } else {
- for (int j = 1; j < cols; j += 2) {
- sum += ((j * 3) + (i * 3) + 1);
- if (i != 0 && i != rows - 1 && j != 0 && j != cols - 1) {
- sum += ((j * 3) + (i * 3) + 1);
- }
- }
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment