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中检查一个数字是否是evil number。