Advertisement
LiliyaBurlakova

Zig Zag

Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main{
  6.     public static void main(String[]args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int rows = Integer.parseInt(scanner.nextLine());
  10.         int cols = Integer.parseInt(scanner.nextLine());
  11.         long sum = 0;
  12.  
  13.         for (int i = 0; i < rows; i++) {
  14.             String line = scanner.nextLine();
  15.             String[] arrayFromLine = line.split(" ");
  16.             long[] numbers = new long[cols];
  17.             for (int j = 0; j < cols; j++) {
  18.                 numbers[j] = Long.parseLong(arrayFromLine[j]);
  19.             }
  20.             if (i == 0) {
  21.                 for (int j = 0; j < numbers.length; j+=2) {
  22.                     sum += numbers[j];
  23.                 }
  24.             } else if (i == (rows - 1)) {
  25.                 if (i % 2 == 0) {
  26.                     for (int j = 0; j < numbers.length; j+=2) {
  27.                         sum += numbers[j];
  28.                     }
  29.                 } else {
  30.                     for (int j = 1; j < numbers.length; j+=2) {
  31.                         sum += numbers[j];
  32.                     }
  33.                 }
  34.             } else {
  35.                 if (i % 2 == 0) {
  36.                     for (int j = 2; j < numbers.length - 1; j+=2) {
  37.                         sum += (numbers[j] * 2);
  38.                     }
  39.                     sum += numbers[0];
  40.                 } else {
  41.                     for (int j = 1; j < numbers.length - 1; j+=2) {
  42.                         sum += (numbers[j] * 2);
  43.                     }
  44.                     sum += numbers[numbers.length - 1];
  45.                 }
  46.             }
  47.         }
  48.         System.out.println(sum);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement