打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。

2014-11-24 07:17:24 · 作者: · 浏览: 4
import java.util.Scanner;


public class Daffodil {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
System.out.println("请输入开始值:");
int a = in.nextInt();
System.out.println("请输入末尾值:");
int b = in.nextInt();

String str = new String();
char[]buff = new char[3];
int plot;
for(int i=a;i
plot=0;
int temp;
buff=(Integer.toString(i)).toCharArray();
for(int j=0;j<3;j++){
temp=Integer.parseInt(String.valueOf(buff[j]));
plot+=Math.pow(temp,3);
}
if(plot==i){
System.out.print(i+" ");
}
}
}
}

摘自 djw10962000的专栏