• 九度OJ 1001 A+B for Matrices


    题目链接:http://ac.jobdu.com/problem.php?pid=1001

    题目分析:

    题目看起来要耐心,题目写的很难懂,看懂了之后会发现原来是一道非常简单的题目。

    首先输入矩阵的行列数,然后输入矩阵的信息,这里要输入两个一样行列数的矩阵,然后将两个矩阵相加,分别计算相加之后的得到的新矩阵的每一行,每一列都为0的个数,然后得到为0个数的总和。

    源代码:

    #include<iostream>
    using namespace std;
    
    int main()
    {
    
    	int i,j;	//行列数
    	int k = 0;	//行,列和为0的个数
    	int sum1 = 0, sum2 = 0;	//行的和,列的和
    	int a1[100][100] = {0}, a2[100][100] = {0};//二维数组保存矩阵信息
    
    	while(1)
    	{
    		cin>>i;
    		if (i == 0)
    		{
    			break;	//输入0时程序结束
    		}
    		cin>>j;
    		//输入第一个矩阵
    		for(int n1 = 0; n1 < i; n1++)
    		{
    			for(int n2 = 0; n2 < j; n2++)
    			{
    				cin>>a1[n1][n2];
    			}
    		}
    		//输入第二个矩阵
    		for(int n1 = 0; n1 < i; n1++)
    		{
    			for(int n2 = 0; n2 < j; n2++)
    			{
    				cin>>a2[n1][n2];
    			}
    		}
    
    		//计算每一行相加后为0的个数
    		for(int n1 = 0; n1 < i; n1++)
    		{
    			for (int n2 = 0; n2 < j; n2++)
    			{
    				sum1 = sum1 + a1[n1][n2] + a2[n1][n2];
    			}
    			if (sum1 == 0)
    			{
    				k++;
    			}
    			sum1 = 0;
    
    		}
    
    		//计算每一列相加后为0的个数
    		for(int n2 = 0; n2 < j; n2 ++)
    		{
    			for (int n1 = 0; n1 < i; n1 ++)
    			{
    				sum2 = sum2 + a1[n1][n2] + a2[n1][n2]; 
    			}
    			if (sum2 == 0)
    			{
    				k++;
    			}
    			sum2 = 0;
    
    		}
    
    		cout<<k<<endl;
    		k = 0;
    	}
    
    	return 0;
    }


  • 相关阅读:
    硬盘安装FreeBSD 6.1release步骤
    Centos,bash: service: command not found
    test1tset
    ubuntu只能访问部份网站的处理方法
    lamp lnmp
    调查用QQ企业邮箱的smtp需要添加spf1
    asp.net文件下载
    FreeBSD更新ports源
    ubuntu 12.10 安装 fcitx 五笔
    csh/tcsh颜色配置
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3080521.html
Copyright © 2020-2023  润新知