【发布时间】:2018-07-18 17:07:03
【问题描述】:
我需要检查一个矩阵的行是否等于第二个矩阵的行。我尝试了几种不同的方法,但似乎找不到一个有效的简单方法。
这是我用于测试的文件:
public static void main(String[] args) {
int[][] a0 = null;
int[][] b0 = null;
int[][] a1 = {};
int[][] b1 = {};
System.out.println(ex1(a0,b0)==false);
System.out.println(ex1(a0,b1)==false);
System.out.println(ex1(a1,b0)==false);
System.out.println(ex1(a1,b1)==false);
int[][] a2 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b2 = {{4, 7 , 1 }
,{3, 5 , 10 }
,{2, 6 , 100}};
System.out.println(ex1(a2,b2)==true );
int[][] a3 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b3 = {{4, 1 , 7}
,{3, 10 , 6}
,{2, 0 , 5}};
System.out.println(ex1(a3,b3)==false);
int[][] a4 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b4 = {{1 , 4 , 7}
,{10, 3 , 6}
,{0 , 2 , 5}};
System.out.println(ex1(a4,b4)==false);
int[][] a5 = {{1 , 2, 5 }
,{10 , 3, 6 }
,{100, 4, 7 }};
int[][] b5 = {{1 , 4, 1 }
,{10 , 3, 10 }
,{0 , 2, 100}};
System.out.println(ex1(a5,b5)==true );
int[][] a6 = {{1 , 2 , 5}
,{10 , 3 , 6}
,{100, 4 , 7}};
int[][] b6 = {{1 , 1 , 7}
,{1 , 10 , 6}
,{0 , 100, 5}};
System.out.println(ex1(a6,b6)==true );
}
}
这是我的代码:
public static boolean ex1(int[][] a, int[][] b){
if(a == null || a.length < 1 || b == null || b.length < 1){
return false;
}
for(int i = 0; i < a.length; i++){
int cont = 0;
for(int j = 0; j < a.length; j++){
for(int z = 0; z < a.length; z++){
if(a[j][i] == b[z][i]){
cont++;
}
if(cont == a[i].length){
return true;
}
}
}
}
return false;
}
这里是测试文件的输出,应该都是真的:Output Image
有人可以帮帮我吗?
【问题讨论】: