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:使用使用者自定義方法和靜態輸入值
在這種方法中,我們宣告一個變數並初始化一個數字作為值,並將此數字作為引數傳遞給使用者自定義方法,然後在方法內部使用演算法來檢查該數字是否為自描述數。
示例
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 中檢查一個數字是否為自描述數。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP