如何在Java中檢查一個數是否為邪惡數?


如果一個數的二進位制表示中1的個數為偶數,則稱該數為邪惡數。

為了更清楚地說明,我們首先需要將給定的數字轉換為二進位制數。轉換後,我們需要計算其中1的個數。如果1的個數為偶數,則可以認為給定的數字是邪惡數。如果1的個數為奇數,則可以認為給定的數字是討厭數

在這篇文章中,我們將瞭解如何使用Java程式語言來檢查一個數是否為邪惡數。

舉幾個例子

示例1

輸入數字為20

讓我們使用邪惡數的邏輯來檢查它

20的二進位制表示為= 10100

1的個數= 2。

我們注意到這裡得到的是一個偶數

因此,20是一個邪惡數

示例2

輸入數字為55。

讓我們使用邪惡數的邏輯來檢查它

55的二進位制表示為= 110111

1的個數= 5。

我們注意到這裡得到的是一個奇數。

因此,55不是一個邪惡數。

示例3

輸入數字為112。

讓我們使用邪惡數的邏輯來檢查它。

112的二進位制表示為= 1110000

1的個數= 3。

我們注意到這裡得到的是一個奇數。

因此,112不是一個邪惡數。

其他一些邪惡數的例子包括0、3、5、6、9、10、12、15、17、18、20、23、24、27、29,等等。

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

  • 步驟2 - 首先將輸入數字轉換為二進位制數並將其儲存到一個變數中。

  • 步驟3 - 然後迴圈,直到二進位制數等於零。

  • 步驟4 - 在每次迭代中,我們檢查個位數是否為1,並在計數後也移除個位數。

  • 步驟5 - 最後,無論我們得到什麼計數值,都檢查該數字是偶數還是奇數。

  • 步驟6 - 如果是偶數,則我們得出結論,該數字是邪惡數,否則該數字不是邪惡數。

多種方法

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

  • 使用靜態輸入值

  • 使用使用者定義的方法

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

方法1:使用靜態輸入值

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

示例

import java.util.*; import java.io.*; public class Main{ //main method public static void main(String[] args){ //declare a variable whcih store the static input int inputNumber = 15; //declare other variables for calculations long binaryOfInputNumber = 0; int temp= inputNumber; int reminder = 0; int i = 1; //binary conversion while(temp != 0){ //find the unit place number reminder = temp % 2; binaryOfInputNumber += reminder * i; temp = temp / 2; i = i * 10; } // for counting the number of 1's we declare a variable int count = 0; //take a loop with a condition till the value is zero while(binaryOfInputNumber != 0){ // check whether the unit place consisting 1 or not if(binaryOfInputNumber % 10 == 1) //increment the count value count++; // after counting removing the unit place in each iteration binaryOfInputNumber = binaryOfInputNumber / 10; } // conditon for checking the count value is even or odd // If even then the number is an evil number otherwise not if(count % 2 == 0) //return true if satisfied System.out.println(inputNumber + " is an evil number"); //return false if not satisfied else System.out.println(inputNumber + " is not an evil number"); } }

輸出

15 is an evil number

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

在這種方法中,將要求使用者輸入輸入數字,並將此數字作為引數傳遞給使用者定義的方法,然後在方法內部使用該演算法來檢查該數字是否為邪惡數。

示例

public class Main { //main method public static void main(String[] args){ //initialize input value int num = 56; System.out.println("Enter a number : "+num); //pass the input value into our user defined method //if its return true then it's an evil number if(checkEvil(num)) System.out.println(num + " is an evil number"); else System.out.println(num + " is not an evil number"); } //define the user defined method public static boolean checkEvil(int inputNumber){ //convert number to binary //the method return a string value String str = Integer.toBinaryString(inputNumber); //type casting string to Integer //store it into a variable long binaryOfInputNumber= Long.parseLong(str); // for counting the number of 1's we declare a variable int count = 0; //binary conversion while(binaryOfInputNumber != 0){ // check whether the unit place consisting 1 or not if(binaryOfInputNumber % 10 == 1) //increment the count value count++; // after counting removing the unit place in each iteration binaryOfInputNumber = binaryOfInputNumber / 10; } // conditon for checking the count value is even or odd if(count % 2 == 0) //return true if satisfied return true; //return false if not satisfied return false; } }

輸出

Enter a number : 56
56 is not an evil number

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

更新於: 2022年10月27日

3K+ 閱讀量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.