如何在Java中檢查一個數是否為水仙花數?
如果一個數的每個位數的n次冪之和等於該數本身,則稱該數為水仙花數,其中n是該數的位數。
更清晰地說,如果一個數有n位,我們首先計算每個位數的n次冪。然後將這些值相加,如果計算出的值與原數相同,則該原數被稱為水仙花數。
一些水仙花數的例子是:153, 8208, 4210818, ... 等。
在這篇文章中,我們將學習如何使用Java程式語言來檢查一個數是否為水仙花數。
舉幾個例子
例項1
輸入數字是153。
讓我們用水仙花數的邏輯來檢查它。
153的位數是3。
計算冪值並相加 = (1^3) + (5^3) + (3^3) = 1 + 125 + 27 = 153
我們在這裡注意到計算出的數字和原數字相同。
因此,153是一個水仙花數。
例項2
輸入數字是8208。
讓我們用水仙花數的邏輯來檢查它。
8208的位數是4。
計算冪值並相加 = (8^4) + (2^4) + (0^4) + (8^4) = 4096 + 16 + 0 + 4096 = 8208
我們在這裡注意到計算出的數字和原數字相同。
因此,8208是一個水仙花數。
例項3
輸入數字是250。
讓我們用水仙花數的邏輯來檢查它。
250的位數是3。
計算冪值並相加 = (2^3) + (5^3) + (0^3) = 8 + 125 + 0 = 133。
我們在這裡注意到計算出的數字和原數字不同。
因此,250不是一個水仙花數。
演算法
步驟1 - 透過靜態輸入方法獲取輸入數字。
步驟2 - 計算原數字的位數。
步驟3 - 初始化一個迴圈來提取數字並計算它的冪值。在迴圈中,將這些計算出的冪值相加並存儲到一個變數中。
步驟4 - 將計算出的值與原數字進行比較。
步驟5 - 如果兩者相同,則輸入數字被稱為水仙花數,否則不是。
語法
在Java中,我們可以使用內建的java.lang.Math.pow()方法來獲取任何數的另一個數的冪。
以下是使用該方法獲取2的冪的語法:
double power = Math.pow (inputValue,2)
多種方法
我們提供了多種方法的解決方案。
使用靜態輸入值和使用者自定義方法
使用使用者輸入和使用者自定義方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值
在這種方法中,我們宣告一個變數並用一個數字初始化它。透過將此數字作為引數呼叫使用者自定義方法。然後,在方法內部,我們可以使用演算法來檢查該數字是否為水仙花數。
示例
import java.util.*; import static java.lang.Math.*; public class Main { public static void main(String args[]) { //declare a variable and initialize it with a number int inputNumber = 8208; //call the function inside the if condition which checks narcissistic if (checkNarcissistic(inputNumber)) System.out.println(inputNumber + " is a narcissistic number."); else System.out.println(inputNumber + " is not a narcissistic number."); } //user-defined method to count the digits static int countDig(int num) { if (num == 0) return 0; return 1 + countDig(num / 10); } //user-defined method to check narcissistic number static boolean checkNarcissistic(int num) { //declare a variable and store the counts of digits int p = countDig(num); //declare a variable and temporary store the input number int temp = num; //Declare a variable which store the sum value and initiate it with 0 int sum = 0; //execute the loop while(temp > 0) { //extarct digits and find power of it //continuously add it to sum value sum+= pow(temp % 10, p); //removes the digit which is already calculated temp = temp / 10; } //return true if original number is equal to the sum value return (num == sum); } }
輸出
8208 is a narcissistic number.
方法2:使用靜態輸入值
在這種方法中,我們宣告一個變數,並透過使用者輸入為其賦值。透過將此數字作為引數呼叫使用者自定義方法。然後,在方法內部,我們可以使用演算法來檢查該數字是否為水仙花數。
示例
import java.util.*; import static java.lang.Math.*; public class Main { public static void main(String args[]) { //create object of Scanner class Scanner sc = new Scanner(System.in); //ask the user to enter a number System.out.print("Enter the number: "); //declare a variable and store the input value int inputNumber = sc.nextInt(); //call the function inside the if condition which checks narcissistic if (checkNarcissistic(inputNumber)) System.out.println(inputNumber + " is a narcissistic number."); else System.out.println(inputNumber + " is not a narcissistic number."); } //user-defined method to count the digits static int countDig(int num) { if (num == 0) return 0; return 1 + countDig(num / 10); } //user-defined method to check narcissistic number static boolean checkNarcissistic(int num) { //declare a variable and store the counts of digits int p = countDig(num); //declare a variable and temporary store the input number int temp = num; //Declare a variable which store the sum value and initiate it with 0 int sum = 0; //execute the loop while(temp > 0) { //extarct digits and find power of it //continuously add it to sum value sum+= pow(temp % 10, p); //removes the digit which is already calculated temp = temp / 10; } //return true if original number is equal to the sum value return (num == sum); } }
輸出
Enter the number: 8208 8208 is a narcissistic number.
在這篇文章中,我們探討了如何使用不同的方法在Java中檢查一個數是否為水仙花數。