The sum of the edge elements of the matrix in Coursera C language Advanced exercise calculation

Source: Internet
Author: User

I've been procrastinating for the last time, and I'm going to keep it up today.

Programming Title #: Calculating the sum of the edge elements of a matrix

Source: POJ (Coursera statement: The exercises completed on POJ will not be counted into Coursera's final results. )

Note: Total time limit: 1000ms memory limit: 65536kB description

Enter an integer matrix to compute the sum of elements at the edge of the matrix. The elements of the so-called matrix edges are the elements of the first and last lines and the first and last columns. input

The first behavior integer k, which indicates that there are K-group data.

Each set of data has a multiple-row composition that represents a matrix:

The first row is the number of rows m and the number of columns N (M < 100,n < 100) of the Matrix, separated by a space.

In the next line of M data entered, each row contains n integers, and the integers are separated by spaces.

Output

Outputs the edge element of the corresponding matrix and a row. 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


Directly paste code:

#include <iostream>
using namespace std;
int main () {
	int t;
	CIN >> T;
	for (int i = 0; i < T; i++) {
		int m = 0, n = 0;
		CIN >> m >> N;
		int shuzu[100][100];//matrix max 100*100
		int sum = 0;
		for (int j = 0; J < m; J +) {for
			(int o = 0; o < n; o++) {
				cin >> shuzu[j][o];//Input Array Value
				if (j = = 0 | | j = = m-1)//First calculate the first line, the last line
					sum + = Shuzu[j][o];
				else if (o==0| | o==n-1)//else and then use if to find other than four corners of the first column, the last column
					sum + = Shuzu[j][o];
			}
		cout << sum << endl;
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.