JavaPTA练习题 7-5 输出所有的水仙花数

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

举例:153就是一个水仙花数。

153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153

请注意:含有main方法的类(class)的名字必须命名为Main,否则调试不成功。

输入格式:

不需要输入。

输出格式:

每一行输出一个水仙花数。

输入样例:

不需要输入。

 
 

输出样例:

在这里给出相应的输出。例如:

153
370
371
407

---------------------------------------------------------------------------------------------------------------------------------

具体代码如下:

public class Main{
	public static void main(String[] args){
		int i=100;
		while(i<1000)
		{
			int ge=i%10;
			int shi=i/10%10;
			int bai=i/100%10;
			if(Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3)==i)
				System.out.println(i);
			i++;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Chen298/article/details/133966336
今日推荐