求1000以内的水仙花数 Java小程序

如题所述

第1个回答  2014-12-28

求1000以内的水仙花数的Java程序如下:

//求1000以内的水仙花数
class ShuiXianHua{
  public static void main(String args[]){
   int a,b,c,n;
   for(n=100;n<=1000;n++){
    a=n%10;
    b=n/10%10;
    c=n/100%10;
    if(n==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3))
     System.out.print(n+"  ");
   }
   }
}

运行结果:

153  370  371  407 

本回答被网友采纳
第2个回答  推荐于2017-04-23
/**
 * 2014年12月28日下午7:27:47
 * @author season
 *
 */
public class FlowerNum {

public static void main(String[] args){
int fg=0;
for(int i=100; i<1000; i++){
if(i==getNum(i)){
if(fg++%5==0) System.out.println();//输出5个换行
System.out.print("   "+i);
}
}
}

/**
 * getNum TODO 求出yourNum每一个数位的立方之和 
 * @param yourNum
 * @return int
 */
public static int getNum(int yourNum){
int num=0;

while(yourNum>0){
int temp =yourNum%10;
num+=temp*temp*temp;
yourNum/=10;
}

return num;
}

}

第3个回答  2014-12-28
public class ShxhNum {
public static void main(String[] args) {
int a, b, c, i; System.out.println("你要找的水仙花数为: ");
for (i = 100; i < 1000; i++) {
a = i / 100;
b= (i / 10) % 10;
c = i % 10;
if (i == a * a * a + b * b * b + c * c * c) {
System.out.println(i);
}
}
}
}