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<b;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+" ");
}
}
}
}
打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
最新推荐文章于 2020-07-02 20:30:11 发布
本文介绍了一个使用Java编写的程序,该程序能够找出用户指定范围内的所有水仙花数。水仙花数是指一个三位数,其各位数字立方和等于该数本身。通过输入起始值和结束值,程序会遍历此区间并检查每个数字是否为水仙花数。
276

被折叠的 条评论
为什么被折叠?



