【题目描述】
The number of daffodils refers to an n-digit number (n≥3), the sum of the n-th power of the digits in each of its digits, etc. On its own. (For example: 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153). Program to find out all three daffodils.
【题目翻译】
水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)。编程求出所有三位的水仙花数。
【本题答案】
/**
* @author yesr
* @create 2018-02-12 下午9:51
* @desc
**/
public class Test0211 {
public static void main(String[] args) {
for(int num=100;num<1000;num++){
if(sxh(num)){
System.out.println(num);
}
}
}
//判断一个数是不是水仙花数
private static boolean sxh(int num){
int b=num/100;
int s=num%100/10;
int g=num%10;
return Math.pow(b, 3)
+ Math.pow(s, 3)
+ Math.pow(g, 3) == num;
}
}