- 列举所有的“四叶玫瑰数”。(一个四位数,个各位的数字的四次方的和,为该数字本身)
public class test {
public static void main(String[] args) {
for(int i = 1000; i<=9999 ; i++){
int a,b,c,d;
a = i/1000;//千位
b = i%1000/100; //百位
c = i%100/10; //十位
d = i%10; //个位
if( i == a*a*a*a +b*b*b*b+c*c*c*c+d*d*d*d){
System.out.println(i);
}
}
}
二.java幂函数的调用
public class test {
public static void main(String[] args) {
for(int i = 1000; i<=9999 ; i++){
int a,b,c,d;
a = i/1000;//千位
b = i%1000/100; //百位
c = i%100/10; //十位
d = i%10; //个位
if( i == Math.pow(a, 4) +Math.pow(b, 4)+Math.pow(c, 4)+Math.pow(d, 4)){
System.out.println(i);
}
}
}