码迷,mamicode.com
首页 > 其他好文 > 详细

水仙花数

时间:2016-05-13 08:41:31      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

水仙花数

时间限制:1000 ms  |  内存限制:65535 KB
难度:0
 
描述
请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。
 
输入
有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)
输入0表示程序输入结束。
输出
如果n是水仙花数就输出Yes
否则输出No
样例输入
153
154
0
样例输出
Yes
No

import java.util.Scanner;


public class Main13 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while (input.hasNext()) {
			int num = input.nextInt();
			if (num == 0) {
				break;
			}
			
			String result = solove(num);
			System.out.println(result);
			
		}
		input.close();
	}

	private static String solove(int num) {
		String result = "";
		int ge = num%10;
		int qian = num/100;
		int bai = num/10%10;
		
		if (Math.pow(ge, 3) +Math.pow(qian, 3) + Math.pow(bai, 3) == num) {
			result = "Yes";
		}else{
			result = "No";
		}
		
		
		return result;
	}
	
}

  

水仙花数

标签:

原文地址:http://www.cnblogs.com/airycode/p/5485386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!