秋馑受刑:谁能用java给我编一个求1000以内的水仙花数的程序呢?

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 05:47:22

JBuilder2006编译运行通过

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author ╰☆╮江水寒
* @version 1.0
*/
//求水仙花数(其个位数十位数百位数的三次方和等于它本身的三位数)
public class NarcissusNumber {
public static void main(String[] args) {
for (int n = 100; n < 1000; n++) {
int i = n / 100; //百位数
int j = (n - i * 100) / 10; //十位数
int k = n - i * 100 - j * 10; //个位数
if (n == i * i * i + j * j * j + k * k * k) {
System.out.print(" " + n);
}
}
}
}

结果:
153 370 371 407

public class Flower{
public static void main(String[] args){
for(int i=100;i<1000;i++){
int a=i/100;
int b=(i%100)/10;
int c=(i%100)%10;
if(f(i,a,b,c)){
System.out.println(""+a+b+c);
}
}
}
public static boolean f(int i,int a,int b,int c){
int s=(int)(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3));
if(i==s){
return true;
}
else{
return false;
}
}
}

自己写的,能达到你的要求,不过算法不怎么好