输入NxM矩阵,矩阵元素均为整数,计算其中大于零的元素之和。
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int a[10][10]; //输入数据 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(a[i][j] >0) { sum += a[i][j]; } } } printf("%d", sum); return 0; }
#include <stdio.h> int main() { int a, b; int sum = 0; scanf("%d %d",&a,&b); int arr[a][b]; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { scanf("%d",&arr[i][j]); if (arr[i][j] > 0){ sum += arr[i][j]; } } } printf("%d\n",sum); return 0; } // // Created by Wilbur Lee on 27/6/2024. //
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int arr[n][m]; int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &arr[i][j]); if (arr[i][j] < 0) { arr[i][j] = 0; } sum = sum + arr[i][j]; } } printf("%d", sum); return 0; }
#include <stdio.h> int main() { int m,n,a[110],b=0,sum=0; scanf("%d %d",&m,&n); while (scanf("%d", &a[b]) != EOF){ if(a[b]>0)sum+=a[b]; b++; } printf("%d",sum); return 0; }
#include<stdio.h> int main() { int arr[100] = {0},n,m,sum = 0; scanf("%d %d",&n,&m); for(int i = 0;i < n*m;i++) { scanf("%d",&arr[i]); if(arr[i] > 0) sum += arr[i]; } printf("%d\n",sum); return 0; }