POJ C Programming Advanced Programming Question # #: Calculating the sum of the edges of matrices

Source: Internet
Author: User

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

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.