//实现判断一个整数是否是水仙花数
//水仙花数:一个三位数,其各个位上的数字立方等于其本身
//such as: 153 = 1*1*1 + 3*3*3 + 5*5*5
import java.util.Scanner;
public class Homework02{
public static void main(String[] args){
//实现判断一个整数是否是水仙花数
//水仙花数:一个三位数,其各个位上的数字立方等于其本身
//such as: 153 = 1*1*1 + 3*3*3 + 5*5*5
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int hundred = x / 100;
int ten = (x - hundred * 100) / 10;
int bit = x - hundred * 100 - ten * 10;
if(x == hundred*hundred*hundred + ten*ten*ten + bit*bit*bit){
System.out.println(x + "是个水仙花数");
}
else{
System.out.println(x + "不是个水仙花数");
}
}
}
该博客介绍了如何使用Java编程语言实现判断一个整数是否为水仙花数,通过计算每一位数字的立方并验证它们之和是否等于原数。
1068

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



