行列の境界要素を印刷するJavaプログラム
この記事では、行列の境界要素を印刷する方法を理解します。マトリックスは、行と列の要素を表したものです。境界要素は、4方向すべての要素に囲まれていない要素です。たとえば、最初の行、最初の列、最後の行、最後の列の要素。
以下は同じのデモンストレーションです-
入力がであると仮定します −
The input matrix: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5
必要な出力は −
The border elements of the matrix is: 9 8 9 8 8 7 7 6 6 5 6 5
アルゴリズム
Step 1 - START Step 2 - Declare an integer matrix namely input_matrix, an object of the class BoundaryElements namely border_values. Step 3 - Define the values. Step 4 - Iterate over each element of the matrix using two for-loops and check if the element is a boundary element using Boolean OR condition. Step 5 - Display the boundary elements. Step 5 - Stop
例1
ここでは、ユーザーがプロンプトに基づいて入力を入力しています。
public class BoundaryElements {
public static void main(String[] args) {
int input_matrix[][] = new int[][] {
{ 9, 8, 9, 8 },
{ 8, 7, 8, 7 },
{ 7, 6, 7, 6 },
{ 6, 5, 6, 5 }
};
System.out.println("The matrix is defined as: ");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
System.out.print(input_matrix[x][y] + " ");
}
System.out.println();
}
BoundaryElements border_values = new BoundaryElements();
System.out.println("The border elements of the matrix is:");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
if (x == 0 || y == 0 || x == input_matrix.length - 1
|| y == input_matrix[x].length - 1) {
System.out.print(input_matrix[x][y] + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
} 出力
The matrix is defined as: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5 The border elements of the matrix is: 9 8 9 8 8 7 7 6 6 5 6 5
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class BoundryElements {
public void Boundary_Elements(int input_matrix[][]) {
System.out.println("The matrix is defined as: ");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
System.out.print(input_matrix[x][y] + " ");
}
System.out.println();
}
System.out.println("The border elements of the matrix is:");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
if (x == 0 || y == 0 || x == input_matrix.length - 1
|| y == input_matrix[x].length - 1) {
System.out.print(input_matrix[x][y] + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
int input_matrix[][] = new int[][] {
{ 9, 8, 9, 8 },
{ 8, 7, 8, 7 },
{ 7, 6, 7, 6 },
{ 6, 5, 6, 5 }
};
BoundryElements border_values = new BoundryElements();
border_values.Boundary_Elements(input_matrix);
}
} 出力
The matrix is defined as: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5 The border elements of the matrix is: 9 8 9 8 8 7 7 6 6 5 6 5
-
Pythonで行列要素をスパイラル順に印刷するプログラム
2Dマトリックスマットがあるとします。行列要素をらせん状に印刷する必要があります。最初に最初の行(mat [0、0])から始めて、コンテンツ全体を印刷し、最後の列に続いて印刷し、最後の行というように、要素をらせん状に印刷します。 したがって、入力が次のような場合 7 10 9 2 9 1 6 2 3 9 1 4 2 7 5 9 9 11 その場合、出力は[7、10、9、1、3、4、5、11、9、9、2、9、6、2、9、2、1、7]になります。 これを
-
行列をZ形式で印刷するPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 −次数n * nの正方行列が与えられた場合、行列の要素をZ形式で表示する必要があります。 Zフォームは、次の手順でマトリックスをトラバースしています- 最初の行をトラバースします 次に、2番目の主対角線を横断します 最後に、最後の行をトラバースします。 ここでは、code.demostrateのフローを示すために暗黙的に取得された入力マトリックスを取得します。 例 arr = [[1, 2, 6, 9], [1, 2, 3, 1], &nb