Java程式建立矩陣並用迴文數填充


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

根據問題陳述,我們必須建立一個矩陣並用迴文數填充它。

讓我們開始吧!

例如

假設我們有一個4*5的矩陣

對矩陣執行操作後,結果將為

輸入行數:4

輸入列數:5

包含迴文數的矩陣

1 2 3 4 5 
6 7 8 9 11 
22 33 44 55 66 
77 88 99 101 111

演算法

步驟1:獲取矩陣的大小。

步驟2:建立一個方陣。

步驟3:檢查迴文數並將其插入矩陣。

步驟4:列印結果。

多種方法

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

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

  • 使用使用者自定義方法

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

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

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法建立矩陣並用迴文數填充它。

示例

import java.util.Scanner;
public class Main 
{
   // main method
   public static void main(String[] args) 
   {
      // taking the size of the matrix
      Scanner scanner = new Scanner(System.in);      
      System.out.print("Enter the number of rows: ");
      int rows = scanner.nextInt();      
      System.out.print("Enter the number of columns: ");
      int columns = scanner.nextInt();      
      // create a square matrix
      int[][] matrix = new int[rows][columns];      
      // Fill the matrix with palindrome numbers
      fillMatrixWithPalindromes(matrix);      
      // Print the matrix
      System.out.println("Matrix with Palindrome Numbers:");
      printMatrix(matrix);
   }   
   // Function to create a matrix and fill it with Palindrome numbers
   public static void fillMatrixWithPalindromes(int[][] matrix) {
      int num = 1;      
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[0].length; j++) {
            // Find the next palindrome number
            while (!isPalindrome(num)) {
               num++;
            }            
            // Fill the matrix with the palindrome number
            matrix[i][j] = num;
            num++;
         }
      }
   }   
   // Function to check if a number is an Palindrome number or not
   public static boolean isPalindrome(int number) {
      String str = String.valueOf(number);
      int i = 0;
      int j = str.length() - 1;      
      while (i < j) {
         if (str.charAt(i) != str.charAt(j)) {
            return false;
         }
         i++;
         j--;
      }      
      return true;
   }   
   // Function to display the matrix
   public static void printMatrix(int[][] matrix) {
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[0].length; j++) {
            System.out.print(matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the number of rows: 4
Enter the number of columns: 5
Matrix with Palindrome Numbers:
1 2 3 4 5 
6 7 8 9 11 
22 33 44 55 66 
77 88 99 101 111

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

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞給使用者自定義方法,並在方法內部根據演算法建立矩陣並用迴文數填充它。

示例

import java.util.Scanner;
public class Main 
{
   // main method
   public static void main(String[] args) 
   {
      // taking the size of the matrix
      Scanner scanner = new Scanner(System.in);      
      System.out.print("Enter the number of rows: ");
      int rows = scanner.nextInt();      
      System.out.print("Enter the number of columns: ");
      int columns = scanner.nextInt();      
      // calling user defined method
      func(rows, columns);   
      
   }  
   // user defined method
   public static void func(int rows, int columns)
   {
      // create a square matrix
      int[][] matrix = new int[rows][columns];
      // Fill the matrix with palindrome numbers
      fillMatrixWithPalindromes(matrix);
      
      // Print the matrix
      System.out.println("Matrix with Palindrome Numbers:");
      printMatrix(matrix);
   }
   // Function to create a matrix and fill it with Palindrome numbers
   public static void fillMatrixWithPalindromes(int[][] matrix) {
      int num = 1;
      
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[0].length; j++) {
            // Find the next palindrome number
            while (!isPalindrome(num)) {
               num++;
            }
            
            // Fill the matrix with the palindrome number
            matrix[i][j] = num;
            num++;
         }
      }
   } 
   // Function to check if a number is an Palindrome number or not
   public static boolean isPalindrome(int number) {
      String str = String.valueOf(number);
      int i = 0;
      int j = str.length() - 1;
     
      while (i < j) {
         if (str.charAt(i) != str.charAt(j)) {
            return false;
         }
         i++;
         j--;
      }
      
      return true;
   } 
   // Function to display the matrix
   public static void printMatrix(int[][] matrix) {
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[0].length; j++) {
            System.out.print(matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the number of rows: 5
Enter the number of columns: 5
Matrix with Palindrome Numbers:
1 2 3 4 5 
6 7 8 9 11 
22 33 44 55 66 
77 88 99 101 111 
121 131 141 151 161

在本文中,我們探討了如何使用Java程式語言建立矩陣並用迴文數填充它。

更新於: 2023年8月17日

218 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告