1. 程式人生 > >1015 水仙花數問題(51Nod)

1015 水仙花數問題(51Nod)

示例 圖片 時間 nod arraylist sys true 它的 output

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)