C#
 Computer >> コンピューター >  >> プログラミング >> C#

2つの行列が同一であるかどうかを確認するC#プログラム


行列が同一であるかどうかを確認するには、最初に行列を比較できるかどうかを確認する必要があります。比較のために、少なくとも2つの行列の次元は同じである必要があるためです。 。

if (row1 != row2 && col1 != col2) {
   Console.Write("Matrices can't be compared:\n");
}

次に、else条件で、メトリックが同一であるかどうかを確認します。ここにもフラグを設定しました-

if (row1 != row2 && col1 != col2) {
   Console.Write("Matrices can't be compared:\n");
} else {
   Console.Write("Comparison of Matrices: \n");
   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!\n\n");
   else
      Console.Write("Our matrices are not equal!");
}

2つの行列が同一であるかどうかを確認するために、完全なコードを見てみましょう。

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:\n");
         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:\n");
         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:\n");
         for (i = 0; i < row1; i++) {
            for (j = 0; j < col1; j++)
            Console.Write("{0} ", arr1[i, j]);
            Console.Write("\n");
         }
         Console.Write("Matrix 2:\n");
         for (i = 0; i < row2; i++) {
            for (j = 0; j < col2; j++)
            Console.Write("{0} ", arr2[i, j]);
            Console.Write("\n");
         }
         if (row1 != row2 &amp;&amp; col1 != col2) {
            Console.Write("Matrices can't be compared:\n");
         } else {
            Console.Write("Comparison of Matrices: \n");
         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!\n\n");
         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!

  1. 2つのリストが循環的に同一であるかどうかをチェックするPythonプログラム

    ここに2つのリストがあります。私たちの仕事は、天気をチェックして、2つのリストが循環的に同一であるかどうかを確認することです。 例 Input : A = [100, 100, 10, 10, 100] B = [100, 100, 100, 10, 10] Output : True 説明 リスト内のこれらの要素が循環的に回転する場合は、他の特定のリストと同様になります。 アルゴリズム Step 1: Create First and Second List. Step 2: Then Lists are converted to map. Step 3: join

  2. 与えられた2つの行列が同一であるかどうかをチェックするPythonプログラム

    ここでは2つの行列が与えられています。両方の行列の順序は同じです。同じように2つの行列が等しくなければならず、両方の行列の行と列の数が等しく、対応する要素も等しくなければなりません。 アルゴリズム Step 1: Create two matrix. Step 2: Then traverse every element of the first matrix and second matrix and compare every element of the first matrix with the second matrix. Step 3: If the both are same