题目描述:
This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.
输入:
The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated
by a space. The first M lines correspond to the elements of A and the second M lines to that of B.
The input is terminated by a zero M and that case must NOT be processed.
输出:
For each test case you should output in one line the total number of zero rows and columns of A+B.
样例输入:
2 2
1 1
1 1
-1 -1
10 9
2 3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
0
样例输出:
1
5
来源:
2011年浙江大学计算机及软件工程研究生机试真题
2,分析
题目大致意思可以翻译为如下:
让矩阵A 和矩阵B相加,判断相加后的矩阵C中,行和列数字全为0的个数。再说得明白点,就是用户先输入要计算的两矩阵行列大小,因为相加,两矩阵行列大小肯定一样。然后依次输入矩阵A和矩阵B的值。输入完成后,自动相加,假设相加结果为矩阵C。先逐行循环统计矩阵C中,是否有一行全为0,若有,则统计数count加1,再逐列循环统计矩阵C中,是否有一行全为0,若有,则统计数count加1,最后输出count的值。
3,答案
import java.util.Scanner;
/**
* to test problem_1001
* @author Sunkun
* Date: 2013.09.24
*/
public class problem_1001{
public static void main(String[] args) {
int row = 0;
int colum = 0;
Scanner input = new Scanner(System.in);
// 输入矩阵的行列大小
while(input.hasNext()){
int m = input.nextInt();
if(m <= 0){
break;
}else{
row = m;
}
if(input.hasNext()){
colum = input.nextInt();
}
// 给A + B中的A赋值
int[][] a;
a = new int[row][colum];
for(int i = 0; i < row; i++){
for(int j = 0; j < colum; j++){
if(input.hasNext()){
a[i][j] = input.nextInt();
}
}
}
// 给A + B中的B赋值
int[][] b;
b = new int[row][colum];
for(int i = 0; i < row; i++){
for(int j = 0; j < colum; j++){
if(input.hasNext()){
b[i][j] = input.nextInt();
}
}
}
// 计算 A + B,得到矩阵C,
int[][] sum;
sum = new int[row][colum];
for(int i = 0; i < row; i++){
for(int j = 0; j < colum; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
int count = 0;
int i = 0,j = 0;
// 统计一行元素是否全为0,若是,则count加1
for(i = 0; i < row; i++){
for(j = 0; j < colum; j++){
if(sum[i][j] != 0){
break;
}
}
if(j == colum){
count++;
}
}
// 统计一列元素是否全为0,若是,则count加1
for(i = 0; i < colum; i++){
for(j = 0; j < row; j++){
if(sum[j][i] != 0){
break;
}
}
if(j == row){
count++;
}
}
// 输出结果
System.out.println(count);
}
}
}
4,备注
上述代码直接复制是AC不了的,把类名problem_1001改为Main就可以直接上传通过了:)
相关推荐
This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.
A{[(CH3)2N]2OPOPO[N(CH3)2]2}(ClO4)2:Mn2+ (A=Mg, Zn) 体系中 (MnO6)10- 配位复合物的局域分子结构:完全能量矩阵研究,卢成,邝小渝,本文介绍了一个简单的研究(MnO6)10- 配位复合物的局域分子结构的理论方法。...
The fundamental theorems on the asymptotic behavior of eigenvalues, inverses, and products of \¯nite section" Toeplitz matrices and Toeplitz ma- trices with absolutely summable elements are derived ...
A well-conditioned estimator for large-dimensional covariance matrices.pdfA well-conditioned estimator for large-dimensional covariance matrices.pdf
Let A and B two n £ n matrices over a ring R (e.g., the reals or the integers) each containing at most m non-zero elements. We present a new algorithm that multiplies A and B using O(m0:7n1:2 + n2+o...
矩阵数据类型和计算器这是一个简单的类定义,它从C ++标准库的向量数据类型派生矩阵数据类型。 我计划随着时间的流逝增加更多功能,并制作出真正的矩阵计算器。当前支持的操作矩阵加/减标量乘法矩阵乘法转置创建身份...
Research in machine learning, statistics and related fields has produced a wide variety of algorithms for classification. However, most of these algorithms assume that all errors have the same cost, ...
This book gives a coherent and detailed description of analytical methods devised to study random matrices. These methods are critical to the understanding of various fields in in mathematics and ...
structural properties of similarity matrices based on two concepts: first, we introduce a new class of robust and scalable audio features which absorb local temporal variations. As a second ...
NULL 博文链接:https://damoqiongqiu.iteye.com/blog/1481207
SDL使用示例和一个YUV文件
outline the basic properties of some matrices associated with a graph. This is followed by topics in graph theory such as regular graphs and algebraic connectivity. Distance matrix of a tree and its ...
Deblurring Images: Matrices, Spectra, and Filtering (Fundamentals of Algorithms 3) (Fundamentals of Algorithms) By Per Christian Hansen Publisher: Society for Industrial and Applied Mathematic ...
Matlab集成的c代码app-network-matrices-2-mat 应用程序示例文档 这是一个如何编写文档的示例(brainlife.io上的Apps的readme.md和license.md) 在这里写以下内容... 该应用程序的功能以及在基本级别上的功能。 用...
Chapter 14: Managing Big Trajectory Data : Online Processing of Positional Streams Chapter 15: Personal Data Protection Aspects of Big Data Chapter 16: Privacy-Preserving Big Data Management : The ...
L-矩阵的一个新的预条件AOR迭代方法,许云霞,雷学红,本文给出了解线性方程组 的一个新的预条件因子 ,应用AOR迭代格式于预条件线性方程组 , 证明了当矩阵 为L-矩阵时此预条件AOR方法是�
机器人学-DH参数法是由Denavit和Hartenberg于1955年提出的一种描述串联式链路上连杆和关节的系统方法。-A kinematic notation for lower-pair mechanisms based on matrices
For readers who already have this knowledge, this book gives a new and practical perspective on the use of regularization methods to solve real problems. Preface; How to Get the Software; List of ...
Tools for big matrix elaboration in OS Window 2000/NT... A useful Excel addin for handling large matrices. Help on-line. Freeware. Bigmatrix supports only the point as decimal separators 内含使用指南!!