public class Day00所有水仙花 {
/**
* @param args
*获取int正数取值范围内的类水仙花数,最大值2147483647另行判断不是类水仙花数!,因为当a=2147483647,a++后是-2147483648;所以共有31个
* 1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
*/
public static void main(String[] args) {
//1.获得所有数
int count = 0;
for(int a = 1;a < 2147483646; a++) { //a=23
//2.判断该数是几位数
int b = 0; //b就是这个数有几位
int c = a; //c=23
while(c != 0) { //23!=0,2!=0,0!=1跳出while循环
b++; //b=1,b=2
c = c / 10; //c=2,c=0
}
//System.out.println(b);
//int a = 12;
int d = 0; //d=0
int g = a; //g=23
int h = 0;
//3.得到该数每个位置上的数
while(g != 0) { //23!=0,2!=0,0!=0跳出循环
d = g % 10; //d=3,d=2
int f = 1; //f=1,f=1
//4.算出每个位置上的位次幂
for(int e = 1; e <= b; e++) {//e=1,e=2,e=3<=b=2跳出循环
f = f * d; //f=3,f=9;f=2,f=4
}
h = h + f;
//System.out.println(f); //f=9,f=4
g = g / 10; //g=2,g=0
}
//System.out.println(h); //f=9,f=4
if(a == h) {
count++;
System.out.println(a);
}
//break;
}
System.out.println("共有" + count + "个");
}
} |
|