Java程式建立矩陣並用阿姆斯特朗數填充


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

根據題意,我們需要建立一個矩陣並用阿姆斯特朗數填充它。

讓我們開始吧!

例如

假設我們有一個4*3矩陣

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

輸入行數:4

輸入列數:3

阿姆斯特朗矩陣

0 1 2 
3 4 5 
6 7 8 
9 153 370 

演算法

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

步驟2:建立一個方陣。

步驟3:檢查阿姆斯特朗數並將其插入矩陣。

步驟4:列印結果。

多種方法

我們提供了不同的方法來解決這個問題。

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

  • 使用使用者自定義方法

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

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

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

示例

import java.util.Scanner;
public class Main 
{
   public static void main(String[] args) 
   {
      // taking 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 = createArmstrongMatrix(rows, columns);
      // print the result
      System.out.println("Armstrong Matrix:");
      displayMatrix(matrix);
   }
   // Function to create a matrix and fill it with Armstrong numbers
   public static int[][] createArmstrongMatrix(int rows, int columns) {
      int[][] matrix = new int[rows][columns];
      int number = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
            while (!isArmstrongNumber(number)) {
               number++;
            }
            matrix[i][j] = number;
            number++;
         }
      }
      return matrix;
   }
   // Function to check if a number is an Armstrong number
   public static boolean isArmstrongNumber(int number) {
      int originalNumber = number;
      int result = 0;
      int digits = String.valueOf(number).length();

      while (number != 0) {
         int remainder = number % 10;
         result += Math.pow(remainder, digits);
         number /= 10;
      }
      return originalNumber == result;
   }
   // Function to display the matrix
   public static void displayMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int element : row) {
            System.out.print(element + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the number of rows: 4
Enter the number of columns: 3
Armstrong Matrix:
0 1 2 
3 4 5 
6 7 8 
9 153 370

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

在這種方法中,矩陣元素將在程式中初始化。然後呼叫使用者自定義方法,並將矩陣作為引數傳遞,並在方法內部根據演算法建立矩陣並用阿姆斯特朗數填充它。

示例

import java.util.Scanner;
public class Main 
{
   // main method
   public static void main(String[] args) 
   {
      // taking 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 = createArmstrongMatrix(rows, columns);
      // print the result
      System.out.println("Armstrong Matrix:");
      displayMatrix(matrix);
   }
   // Function to create a matrix and fill it with Armstrong numbers
   public static int[][] createArmstrongMatrix(int rows, int columns) {
      int[][] matrix = new int[rows][columns];
      int number = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
            while (!isArmstrongNumber(number)) {
               number++;
            }
            matrix[i][j] = number;
            number++;
         }
      }
      return matrix;
   }
   // Function to check if a number is an Armstrong number
   public static boolean isArmstrongNumber(int number) {
      int originalNumber = number;
      int result = 0;
      int digits = String.valueOf(number).length();
      while (number != 0) {
         int remainder = number % 10;
         result += Math.pow(remainder, digits);
         number /= 10;
      }
      return originalNumber == result;
   }
   // Function to display the matrix
   public static void displayMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int element : row) {
            System.out.print(element + " ");
         }
         System.out.println();
      }
   }
}

輸出

Enter the number of rows: 4
Enter the number of columns: 4
Armstrong Matrix:
0 1 2 3 
4 5 6 7 
8 9 153 370 
371 407 1634 8208

在這篇文章中,我們探討了如何使用Java程式語言建立矩陣並用阿姆斯特朗數填充它。

更新於:2023年8月17日

瀏覽量:136

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.