建立矩陣,並用1填充主對角線,用2填充副對角線


在Java中,可以使用二維陣列表示矩陣。矩陣用於儲存和處理具有表格結構的資料。矩陣具有與其相關的幾個屬性和運算,例如加法、減法、乘法、轉置和行列式計算。

根據題目要求,我們需要建立一個矩陣,並用1填充主對角線,用0填充副對角線。

讓我們開始吧!

例如

假設我們有一個4*4的矩陣,即4行4列。

對矩陣進行運算後,結果將是

輸入矩陣的大小:4

結果矩陣是

1 0 0 0 
0 1 0 0 
0 0 1 0 
0 0 0 1 

演算法

步驟1:定義矩陣的大小。

步驟2:建立一個大小為“size”的方陣。

步驟3:在主對角線上填充1,在副對角線上填充0。

步驟4:列印結果。

多種方法

我們提供了不同方法的解決方案。

  • 使用矩陣元素的靜態初始化

  • 使用使用者自定義方法

讓我們一一檢視程式及其輸出。

方法1:使用陣列元素的靜態初始化

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法建立矩陣,並用1填充主對角線,用0填充副對角線。

示例

import java.util.Scanner;
public class Main 
{
   public static void main(String[] args) 
   {
      Scanner scanner = new Scanner(System.in);
      // Define the size of the matrix
      System.out.print("Enter the size of the matrix: ");
      int size = scanner.nextInt();
      // Create a square matrix of size 'size'
      int[][] matrix = new int[size][size]; 
      // Filling the matrix 
      for (int i = 0; i < size; i++) {
         for (int j = 0; j < size; j++) {
            if (i == j) {
               // Filling 1 on the primary diagonal
               matrix[i][j] = 1; 
            } else if (i + j == size - 1) {
               // Filling 0 on the secondary diagonal
               matrix[i][j] = 0; 
            }
         }
      }
      // Print the matrix
      System.out.println("The resultant matrix is:");
      for (int i = 0; i < size; i++) {
         for (int j = 0; j < size; j++) {
            System.out.print(matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the size of the matrix: 4
The resultant matrix is:
1 0 0 0 
0 1 0 0 
0 0 1 0 
0 0 0 1

方法2:使用使用者自定義方法

在這種方法中,矩陣元素將在程式中初始化。然後呼叫使用者自定義方法,並將矩陣作為引數傳遞,並在方法內部根據演算法建立矩陣,並用1填充主對角線,用0填充副對角線。

示例

import java.util.Scanner;
public class Main 
{
   public static void main(String[] args) 
   {
      // taking the dimension of the matrix 
      Scanner scanner = new Scanner(System.in);
      // Define the size of the matrix
      System.out.print("Enter the size of the matrix: ");
      int size = scanner.nextInt();
      // calling user defined method
      func(size);   
   }
   // user defined method
    public static void func(int size)
   {
      // Create a square matrix of size 'size'
      int[][] matrix = new int[size][size]; 
      // Filling the matrix 
      for (int i = 0; i < size; i++) {
         for (int j = 0; j < size; j++) {
            if (i == j) {
               // Filling 1 on the primary diagonal
               matrix[i][j] = 1; 
            } else if (i + j == size - 1) {
               // Filling 0 on the secondary diagonal
               matrix[i][j] = 0; 
            }
         }
      }
      // Print the matrix
      System.out.println("The resultant matrix is:");
      for (int i = 0; i < size; i++) {
         for (int j = 0; j < size; j++) {
            System.out.print(matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the size of the matrix: 5
The resultant matrix is:
1 0 0 0 0 
0 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1

在這篇文章中,我們探討了如何使用Java程式語言建立一個矩陣,並用1填充主對角線,用0填充副對角線。

更新於:2023年8月17日

瀏覽量:281

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告