C/C++ Programming Learning-Week 8 ⑧ Matrix Calculation

Topic link

Title description

Enter the NxM matrix and find the sum of the elements greater than zero.

input:

Multiple sets of test cases,

The first line is NM (N: the number of rows in the matrix; M: the number of columns in the matrix, and M,N<10)

The next N rows of the matrix

output:

Multiple sets of calculation results

Sample Input

3 3
2 3 4
-5 -9 -7
0 8 -4
2 1
1
-1

Sample Output

17
1

Ideas

Input N * M numbers, add up the numbers greater than 0, and output the sum.

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n, m;
	while(cin >> n >> m)
	{
    
    
		int sum = 0, num;
		for(int i = 0; i < n; i++)
		{
    
    
			for(int j = 0; j < m; j++)
			{
    
    
				cin >> num;
				if(num > 0) sum += num;
			}
		}
		cout << sum << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113080028