Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- String[] cord = line.split(" ");
- int[] dimentions = new int[cord.length];
- for (int i = 0; i < dimentions.length; i++) {
- dimentions[i] = Integer.parseInt(cord[i]);
- }
- int rows = dimentions[0];
- int sum = 0;
- for (int i = 0; i < rows; i++) {
- String matrixRow = scanner.nextLine();
- String[] matrixRowArr = matrixRow.split(" ");
- int[] array = new int[matrixRowArr.length];
- for (int j = 0; j < array.length; j++) {
- array[j] = Integer.parseInt(matrixRowArr[j]);
- }
- if (i == 0) {
- for (int j = 0; j < array.length; j += 2) {
- sum += array[j];
- }
- } else if (i == rows - 1) {
- if (i % 2 == 0) {
- for (int j = 0; j < array.length; j += 2) {
- sum += array[j];
- }
- } else {
- for (int j = 1; j < array.length; j += 2) {
- sum += array[j];
- }
- }
- } else {
- if (i % 2 == 0) {
- for (int j = 2; j < array.length - 1; j += 2) {
- sum += ((array[j]) * 2);
- }
- sum += ((array[0]));
- } else {
- for (int j = 1; j < array.length - 1; j += 2) {
- sum += ((array[j]) * 2);
- }
- sum += ((array[array.length - 1]));
- }
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement