给整型二维数组 b[3][4]输入12个数据,计算并输出数组中所有正数之和、所有负数之和。

C语言\C++都可

#include "stdio.h"
main()
{
int b[3][4],i,j,zh=0,fu=0;
printf("please input b[3][4]:\n");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{
scanf("%d",&b[i][j]);
if(b[i][j]>0)zh+=b[i][j];
if(b[i][j]<0)fu+=b[i][j];
}
printf("the sum of those numbers larger than 0 is : %d .\n",zh);
printf("the sum of those numbers smaller than 0 is : %d .",fu);
}

/*运行结果:
please input b[3][4]:
1 -2 3
-3 5 2
-3 -7 6
2 7 4
the sum of those numbers larger than 0 is : 30 .
the sum of those numbers smaller than 0 is : -15 .
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-25
VC6 测试通过

#include<stdio.h>

void main(){
int a[3][4]={10,80,-4,98,-56,16,23,45,-9,45,12,-76};
int minus=0,plus=0;
for(int i=0;i<3;i++)
for(int j=0;j<4;j++){
if(a[i][j]<0)minus+=a[i][j];
else plus+=a[i][j];
}
printf("sum of minus=%d\nsum of plus=%d\n",minus,plus);
}
第2个回答  2008-04-25
#include <iostream>
using namespace std;

int main()
{
int b[3][4];
int asum = 0, nsum = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
{
cin >> b[i][j];
if(b[i][j] > 0)
asum += b[i][j];
else if(b[i][j] < 0)
nsum +=b[i][j];
}
cout << asum << endl;
cout << nsum << endl;

}
第3个回答  2017-06-16
#include<cstdio>

using namespace std;

int b[3][4];

int main(){
int zheng_sum=0,fu_sum=0;
for(int i=0;i<3;++i)for(int j=0;j<4;++j){
scanf("%d",&b[i][j])
if(b[i][j]>0)zheng_sum+=b[i][j];
if(b[i][j]<0)fu_sum+=b[i][j];
}
printf("%d %d",zheng_sum,fu_sum);
}
第4个回答  2008-04-25
这个简单啊。楼主可以自己试着编编。两层循环就行了。