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

1015 水仙花数问题(51Nod)

时间:2018-03-31 23:12:03      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:OLE   scanner   sts   pos   思路   oid   表示   它的   integer   

1015 水仙花数 

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题

水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)

给出一个整数M,求 >= M的最小的水仙花数。

Input

一个整数M(10 <= M <= 1000)

Output

输出>= M的最小的水仙花数

Input示例

99

Output示例

153

 思路:本题常规思路很容易超时,故采用打表来做。

一、常规思路容易超时

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n  = scan.nextInt();
		while(true){
			if(isShuiXianhua(n)){
				break;
			}else{
				n++;
			}
		}
		System.out.println(n);
	}
	
	public static boolean isShuiXianhua(int n){
		int length = String.valueOf(n).length();
		int temp = n;
		int result = 0;
		while(n!=0){
			result+=Math.pow(n%10, length);
			n = n/10;
		}
		if(temp == result)
			return true;
		return false;
	}
}

技术分享图片

技术分享图片

二、生成水仙花数

package Java算法;

public class TestShuiXianHua {
	public static void main(String[] args) {
	
		for(int i=10;i<=1000000000;i++){
			if(isShuiXianhua(i)){
				System.out.println(i);
			}
		}
	}
	
	public static boolean isShuiXianhua(int n){
		int length = String.valueOf(n).length();
		int temp = n;
		int result = 0;
		while(n!=0){
			result+=Math.pow(n%10, length);
			n = n/10;
		}
		if(temp == result)
			return true;
		return false;
	}
}
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153

三、解答

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n  = scan.nextInt();
		while(true){
			if(isShuiXianhua(n)){
				break;
			}else{
				n++;
			}
		}
		System.out.println(n);
	}
	
	public static boolean isShuiXianhua(int n){
		int[] number = {153,370,371,407,1634,8208,9474,54748,92727,93084,548834};
		for(int i = 0;i<number.length;i++){
		    if(number[i] == n){
		        return true;
		    }
		}
		return false;
	}
}

技术分享图片

四、另一道水仙花题目

描述
    请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。

输入
    有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)
输入0表示程序输入结束。

输出
    如果n是水仙花数就输出Yes
否则输出No

样例输入
    153
    154
    0
样例输出
    Yes
    No
package Java算法;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
描述
请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。
输入
有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)
输入0表示程序输入结束。
输出
如果n是水仙花数就输出Yes
否则输出No
样例输入
153
154
0
样例输出
Yes
No
*/
public class 水仙花数 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		List<Integer> li = new ArrayList<Integer>();
		while(n!=0){
			li.add(n);
			n = scan.nextInt();
		}
		
		for(Integer i :li){
			if(isShuiXianhua(i)){
				System.out.println("Yes");
			}else{
				System.out.println("No");
			}
		}
	}
	
	public static boolean isShuiXianhua(int n){
		int length = String.valueOf(n).length();
		int temp = n;
		int result = 0;
		while(n!=0){
			result+=Math.pow(n%10, length);
			n = n/10;
		}
		if(temp == result)
			return true;
		return false;
	}
}

  

 

1015 水仙花数问题(51Nod)

标签:OLE   scanner   sts   pos   思路   oid   表示   它的   integer   

原文地址:https://www.cnblogs.com/hglibin/p/8684437.html

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