Define a A x B sum as the sum of all integers in a A x B grid. Given an N x M grid of integers, sum up all of the A x B sums in that grid. You will be given integers N, M, A, B. Then in the following N lines, you will be given M integers.
Sample input:
4 3 3 2
1 2 3
4 5 6
7 8 9
10 11 12
Sample output:
156
Explanation:
The A x B grids in the given grid are the 3 x 2 grids:
1 2 2 3 4 5 5 6
4 5 5 6 7 8 8 9
7 8 8 9 10 11 11 12
If you sum up all of those numbers, you get 156
winstygu
May 16, 2020
class Main {publicstaticvoid main(String[] args) {int N = 4;int M = 3;int A = 3;int B = 2;int total=0;int[][] nm = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };for (int i=0; i<A; i++){for(int j=0; j<B; j++){ total = total + nm[i][j]; } }for (int i=0; i<A; i++){for(int j=1; j<=B; j++){ total = total + nm[i][j]; } }for (int i=1; i<=A; i++){for(int j=0; j<B; j++){ total = total + nm[i][j]; } }for (int i=1; i<=A; i++){for(int j=1; j<=B; j++){ total = total + nm[i][j]; } } System.out.println("The total is " + total); }}
helen_lan2002
May 16, 2020
class Main {publicstaticvoid main(String[] args) {int N = 4;int M = 3;int A = 3;int B = 2;int count = 0;int[][] NxM = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };for (int y = 0; y < A; y++) {for (int x = 0; x < B; x++) { count = count + NxM[y][x]; } }for (int y = 0; y < A; y++) {for (int x = 1; x <= B; x++) { count = count + NxM[y][x]; } }for (int y = 1; y <= A; y++) {for (int x = 0; x < B; x++) { count = count + NxM[y][x]; } }for (int y = 1; y <= A; y++) {for (int x = 1; x <= B; x++) { count = count + NxM[y][x]; } } System.out.println("Da total is " + count + "."); }}
Class 2:
Define a A x B sum as the sum of all integers in a A x B grid. Given an N x M grid of integers, sum up all of the A x B sums in that grid. You will be given integers N, M, A, B. Then in the following N lines, you will be given M integers.
Sample input:
4 3 3 2
1 2 3
4 5 6
7 8 9
10 11 12
Sample output:
156
Explanation:
The A x B grids in the given grid are the 3 x 2 grids:
1 2 2 3 4 5 5 6
4 5 5 6 7 8 8 9
7 8 8 9 10 11 11 12
If you sum up all of those numbers, you get 156
class Main { public static void main(String[] args) { int N = 4; int M = 3; int A = 3; int B = 2; int total=0; int[][] nm = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }; for (int i=0; i<A; i++){ for(int j=0; j<B; j++){ total = total + nm[i][j]; } } for (int i=0; i<A; i++){ for(int j=1; j<=B; j++){ total = total + nm[i][j]; } } for (int i=1; i<=A; i++){ for(int j=0; j<B; j++){ total = total + nm[i][j]; } }for (int i=1; i<=A; i++){ for(int j=1; j<=B; j++){ total = total + nm[i][j]; } } System.out.println("The total is " + total); } }
class Main { public static void main(String[] args) { int N = 4; int M = 3; int A = 3; int B = 2; int count = 0; int[][] NxM = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; for (int y = 0; y < A; y++) { for (int x = 0; x < B; x++) { count = count + NxM[y][x]; } } for (int y = 0; y < A; y++) { for (int x = 1; x <= B; x++) { count = count + NxM[y][x]; } } for (int y = 1; y <= A; y++) { for (int x = 0; x < B; x++) { count = count + NxM[y][x]; } } for (int y = 1; y <= A; y++) { for (int x = 1; x <= B; x++) { count = count + NxM[y][x]; } } System.out.println("Da total is " + count + "."); } }