java-判断一个数是不是水仙花数
// 水仙花数是指一个大于等于三位的数,它具备特征:每一位的水仙花数的长度次幂之和等于水仙花数
//三位的水仙花数共有4个:153,370,371,407;
//四位的四叶玫瑰数共有3个:1634,8208,9474;
//五位的五角星数共有3个:54748,92727,93084;
//六位的六合数只有1个:548834;
public class Work9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int [] each_bit = get_each_bit(num);
int sum = 0;
for (int i = 0; i < each_bit.length; i++) {
double pow = Math.pow(each_bit[i], each_bit.length);
sum += pow;
}
if (sum == num){
switch (each_bit.length) {
case 3 -> System.out.println("这个数是水仙花数");
case 4 -> System.out.println("这个数是四叶玫瑰数");
case 5 -> System.out.println("这个数是五角星数");
case 6 -> System.out.println("这个数是六合数");
case 7 -> System.out.println("这个数是北斗七星数");
case 8 -> System.out.println("这个数是八仙数");
case 9 -> System.out.println("这个数是九九重阳数");
case 10 -> System.out.println("这个数是十全十美数");
}
} else {System.out.println("这个数不是水仙花数");}
scanner.close();
}
public static int[] get_each_bit(int num){
int l = (num+"").length();
int [] list = new int[l];
int i = 0;
while (num!=0){
list[i] = num % 10; // get last bit
num = num / 10; // delete num last bit
i++;
}
return list;
}
}