您好,欢迎访问一九零五行业门户网

检查两个矩阵是否相同的 C# 程序

要检查矩阵是否相同,您需要首先检查矩阵是否可以比较,因为为了进行比较,至少两个矩阵的维度应该相同。
if (row1 != row2 && col1 != col2) { console.write("matrices can't be compared:");}
现在,在 else 条件下检查指标是否相同。我们还在这里设置了一个标志 -
if (row1 != row2 &amp;&amp; col1 != col2) { console.write("matrices can't be compared:");} else { console.write("comparison of matrices: "); for (i = 0; i < row1; i++) { for (j = 0; j < col2; j++) { if (arr1[i, j] != arr2[i, j]) { flag = 0; break; } } } if (flag == 1) console.write("our matrices are equal!"); else console.write("our matrices are not equal!");}
示例让我们看一下完整的代码来检查两个矩阵是否相同。
现场演示
using system;namespace demo { public class applicationone { public static void main() { int[, ] arr1 = new int[10, 10]; int[, ] arr2 = new int[10, 10]; int flag = 1; int i, j, row1, col1, row2, col2; console.write("rows in the 1st matrix: "); row1 = convert.toint32(console.readline()); console.write("columns in the 1st matrix: "); col1 = convert.toint32(console.readline()); console.write("rows in the 2nd matrix: "); row2 = convert.toint32(console.readline()); console.write("columns in the 2nd matrix: "); col2 = convert.toint32(console.readline()); console.write("elements in the first matrix:"); for (i = 0; i < row1; i++) { for (j = 0; j < col1; j++) { console.write("element - [{0}],[{1}] : ", i, j); arr1[i, j] = convert.toint32(console.readline()); } } console.write("elements in the second matrix:"); for (i = 0; i < row2; i++) { for (j = 0; j < col2; j++) { console.write("element - [{0}],[{1}] : ", i, j); arr2[i, j] = convert.toint32(console.readline()); } } console.write("matrix 1:"); for (i = 0; i < row1; i++) { for (j = 0; j < col1; j++) console.write("{0} ", arr1[i, j]); console.write(""); } console.write("matrix 2:"); for (i = 0; i < row2; i++) { for (j = 0; j < col2; j++) console.write("{0} ", arr2[i, j]); console.write(""); } if (row1 != row2 &amp;&amp; col1 != col2) { console.write("matrices can't be compared:"); } else { console.write("comparison of matrices: "); for (i = 0; i < row1; i++) { for (j = 0; j < col2; j++) { if (arr1[i, j] != arr2[i, j]) { flag = 0; break; } } } if (flag == 1) console.write("our matrices are equal!"); else console.write("our matrices are not equal!"); } } }}
输出rows in the 1st matrix: columns in the 1st matrix: rows in the 2nd matrix: columns in the 2nd matrix: elements in the first matrix:elements in the second matrix:matrix 1:matrix 2:comparison of matrices: our matrices are equal!
以上就是检查两个矩阵是否相同的 c# 程序的详细内容。
其它类似信息

推荐信息