Programming Question # #: Calculating the sum of the edge elements of matrices
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 1000ms memory limit: 65536kB
Describe
Enter an integer matrix to calculate the sum of the elements at the edge of the matrix. The elements of the so-called Matrix Edge are the elements of the first and last lines, and the elements of the first and last columns.
Input
The first behavior, the integer k, indicates that there are k groups of data.
Each set of data consists of multiple lines representing a matrix:
The first line is the number of rows of the matrix M and the number of columns N (M < 100,n < 100), separated by a space.
In the next input m-row data, each row contains n integers, and the integers are separated by spaces.
Output
The output corresponds to the edge element of the matrix and, one line.
Sample input
2
4 4
1 1 1 1
0 0 0 0
1 0 1 0
0 0 0 0
3 3
3 4 1
3 7 1
2 0 1
Sample output
5
15
#include <iostream>#include<stdio.h>using namespacestd;intMain () {intK, L, T; CIN>>K; while(k >0) { intm, N, I, J; intsum =0; CIN>> m >>N; for(i=0; I < m; i++) { for(j=0; J < N; J + +) {cin>>T; if(i==0|| i==m-1|| j==0|| j==n-1) {sum+=T; } }} cout<< sum <<Endl; K--; } return 0;}
POJ C Programming Advanced Programming Question # #: Calculating the sum of the edges of matrices