/**
* 水仙花数是指一个 n 位数 ( n≥3 ),
* 它的每个位上的数字的 n 次幂之和等于它本身。
* (例如:1^3 + 5^3 + 3^3 = 153)
*/
package ma;
public class ShuiXian {
public static void main(String[] args) {
for(int i=100; i<1000; i++){
if (IsShuiXian(i)) {
System.out.println(i);
}
}
}
/**
* 求三位的水仙花数
*/
public static boolean IsShuiXian(int N) {
int b = N/100;
int s = (N % 100) / 10;
int g = (N % 10);
if (Math.pow(b, 3) + Math.pow(s, 3) + Math.pow(g, 3) == N) {
return true;
} else {
return false;
}
}
}
Java实例21 - 水仙花数
最新推荐文章于 2023-03-27 22:41:50 发布