Java 检查一个数是否为自传数
如果一个数的第一个数字表示给定数字中0的个数,第二个数字表示给定数字中1的个数,第三个数字表示给定数字中2的个数,以此类推,那么这个数被称为自传数。
简单地说,如果给定数字从左到右的数字表示给定数字中0、1、2、3、4…. N分别出现的次数,那么这个数就是自传数。
一些自传数的示例是:1210,2020,21200,3211000,42101000…等等。
为了给您展示一些实例-
实例-1
Input number is 1210.
Let’s check it by using the logic of Autobiographical numbers.
Number of zeros available in the given number is = 1. And the first digit is also 1.
Number of one’s available in the given number is= 2. And the second digit is also 2.
Number of two available in the given number is= 1. And the third digit is also 1.
Number of three’s available in the given number is= 0. And the fourth digit is also 0.
As we notice here by arranging all those digits, we will get the same original number.
Hence, 1210 is an Autobiographical number.
实例-2
Input number is 12312.
Let’s check it by using the logic of Autobiographical numbers.
Number of zeros available in the given number is = 0, but the first digit is 1.
Hence, 12312 is not an Autobiographical number.
步骤
步骤1 - 通过静态输入方法获取输入的数字。
步骤2 - 将输入的数字转换为字符串。
步骤3 - 声明一个数组,然后将该字符串的每个数字存储到数组中。
步骤4 - 然后启动循环,检查0、1、2、3、4…n的出现次数。
步骤5 - 如果将各个数字的出现次数组合起来得到的数字与原始数字相同,则打印给定数字是自传数,否则不是。
语法
要在Java中求一个数的幂的绝对值,我们可以使用内置的 java.lang.Math.abs() 方法。
以下是使用该方法求2的幂的语法:
int value = Math.abs (inputValue)
多种方法
我们提供了不同的解决方法。
- 通过用户定义的静态输入值的方法。
-
通过使用用户输入值的方法。
让我们逐个查看程序以及它的输出。
方法1:通过使用用户定义的静态输入值的方法
在这种方法中,我们声明一个变量并将一个数字初始化为value,然后将该数字作为参数传递给一个用户定义的方法,然后在方法内部,通过使用算法来检查该数字是否是自传数字。
示例
import java.util.*;
public class Main {
public static void main(String args[]) {
int inp= 3211000;
if(checkAutobiographical(inp))
System.out.println(inp + " is an autobiographical number.");
else
System.out.println(inp + " is not an autobiographical number.");
}
public static boolean checkAutobiographical(int n){
int inputNumber = Math.abs(n);
int temp = inputNumber;
String s = String.valueOf(inputNumber);
int arr[] = new int[s.length()];
for(int i = arr.length - 1; i >= 0; i--) {
arr[i] = temp % 10;
temp = temp/10;
}
boolean f = true;
for(int i = 0; i < arr.length; i++) {
int count = 0;
for(int j = 0; j < arr.length; j++) {
if(i == arr[j])
count++;
}
if(count != arr[i]) {
f = false;
break;
}
}
if(f)
return true;
else
return false;
}
}
输出
3211000 is an autobiographical number.
方法2:通过使用用户输入值
在此方法中,我们通过用户输入声明一个输入数字,并使用算法来检查该数字是否是自传数字
示例
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int inputNumber = sc.nextInt();
inputNumber = Math.abs(inputNumber);
int temp = inputNumber;
String s = String.valueOf(inputNumber);
int arr[] = new int[s.length()];
for(int i = arr.length - 1; i >= 0; i--) {
arr[i] = temp % 10;
temp = temp/10;
}
boolean f = true;
for(int i = 0; i < arr.length; i++) {
int count = 0;
for(int j = 0; j < arr.length; j++) {
if(i == arr[j])
count++;
}
if(count != arr[i]) {
f = false;
break;
}
}
if(f)
System.out.println(inputNumber + " is an autobiographical number.");
else
System.out.println(inputNumber + " is not an autobiographical number.");
}
}
输出
Enter the number: 2020
2020 is an autobiographical number.
在本文中,我们探讨了如何通过使用不同的方法在Java中检查一个数字是否是自传数字。