输入NxM矩阵,矩阵元素均为整数,计算其中大于零的元素之和。
#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; }
#include<iostream> using namespace std; int main() { int N,M,sum; cin >> N; cin >> M; int a[N][M]; for(int i = 0;i<N;i++) for(int j = 0;j<M;j++){ cin>>a[i][j]; } for(int i = 0;i<N;i++) for(int j = 0;j<M;j++){ if(a[i][j]>0){ sum += a[i][j]; } } cout<<sum; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; int m=sc.nextInt(); int n=sc.nextInt(); int[][] array=new int[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ array[i][j]=sc.nextInt(); if(array[i][j]>0){ num+=array[i][j]; } } } System.out.println(num); } }
#include <stdio.h> #include <stdlib.h> int main(void) { int sum = 0; int i, j, n, m; while (scanf("%d %d", &n, &m) != EOF && (n > 0 && n <= 10) && (m > 0 && m <= 10)) { int (*a)[m] = (int (*)[m])malloc(n * m * sizeof(int)); if (NULL == a) { fprintf(stderr, "Memory allocation failed!\n"); exit(EXIT_FAILURE); } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (a[i][j] > 0) { sum += a[i][j]; } } } printf("%d\n", sum); free(a); a = NULL; } return 0; }//simple problem;
#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 n,m; scanf("%d %d",&n,&m); int i,j,sum=0; int array[n][m]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&array[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(array[i][j]>0) { sum+=array[i][j]; } } } printf("%d",sum); return 0; }