package com.itheima.api.math;
public class MathDemo3 {
//自幂数,一个n位自然数等于自身各个数位上数字的n次幂之和
//要求1:统计一共有多少个水仙花数,输出所有水仙花数
public static void main(String[] args) {
int count =0;
//不一定是100到999,只要不超过int范围都可以
for (int i = 100; i <= 999; i++) {
if (i==constain(i)){
//统计有多少水仙花数
count++;
System.out.println(i);
}
}
System.out.println("一共有"+count+"水仙花数");
}
//返回n位自然数等于自身各个数位上数字的n次幂之和
private static int constain(int number){
int n=0;
int temp =number;
//求出number有多少位
while (number!=0)
{
number/=10;
n++;
}
int sum=0;
for (int i = 0; i < n; i++) {
int ge=temp%10;
temp/=10;
//获取a的b次方
sum+=Math.pow(ge,n);
}
return sum;
}
}