如何在Java中檢查一個數是否為迷人數字?


迷人數字可以定義為一個數字,當它分別乘以2和3後,並將結果與原數字連線在一起,所得到的數字包含從1到9的所有數字。

要成為一個迷人數字,它必須是三位數或三位數以上。

舉幾個例子:

示例1

輸入數字為327

讓我們用迷人數字的邏輯來檢查它:

327 * 2 = 654
327 * 3 = 981

連線“654” + “981” + “327” = 654981327

因此,327是一個迷人數字。

示例2

輸入數字為192

讓我們用迷人數字的邏輯來檢查它:

192 * 2 = 384
192 * 3 = 576

連線“384” + “576” + “192” = 384576192

因此,327是一個迷人數字。

示例3

輸入數字為241

讓我們用迷人數字的邏輯來檢查它:

241 * 2 = 482
241 * 3 = 723

連線“482” + “723” + “241” = 482723241

因此,241不是一個迷人數字。

其他一些迷人數字的例子包括192、1920、2019、327等。

演算法

  • 步驟1 - 獲取一個整數,可以透過初始化或使用者輸入獲得。

  • 步驟2 - 檢查它是否是一個三位數。

  • 步驟3 - 將數字乘以2和3。

  • 步驟4 - 將兩個乘積與原數字連線起來。

  • 步驟5 - 現在查詢數字中是否存在從1到9的所有數字。如果存在,則它是一個迷人數字。

多種方法

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

  • 使用靜態輸入值

  • 使用使用者自定義方法

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

方法1:使用靜態輸入值

在這種方法中,在程式中初始化一個整數值,然後使用該演算法來檢查一個數字是否為迷人數字。

示例

public class Main { public static void main(String args[]){ // Initialized an integer value int num = 327; System.out.println("Given number: "+num); // Store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // Concatenate the numbers String concatNum = prod1+""+prod2+num; // Boolean value to store the result boolean flag = true; // Loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // COunt holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatNum.length(); i++) { char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } // Prints the result if(flag) System.out.println("Fascinating number"); else System.out.println("Not a fascinating number"); } }

輸出

Given number: 327
Fascinating number

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

在這種方法中,在程式中初始化一個整數值,並將此數字作為引數傳遞給使用者自定義方法,然後在方法內部使用該演算法來檢查一個數字是否為迷人數字。

示例

public class Mai { static boolean fascinatingNum(int num){ // Store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // Concatenate the numbers String concatNum = prod1+""+prod2+num; // Boolean value to store the result boolean flag = true; // Loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // COunt holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatNum.length(); i++){ char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } return flag; } public static void main(String args[]){ // Initialized an integer value int num = 327; System.out.println("Given number: "+num); // Calls the user defined method and stores the result in res variable boolean res = fascinatingNum(num); // Prints the result if(res) System.out.println("Fascinating number"); else System.out.println("Not a fascinating number"); } }

輸出

Given number: 327
Fascinating number

在這篇文章中,我們探討了如何使用不同的方法在Java中檢查一個數字是否為迷人數字。

更新於:2022年10月27日

4K+ 瀏覽量

開啟您的職業生涯

透過完成課程獲得認證

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