zoukankan      html  css  js  c++  java
  • (7)如何得到所有的 "水仙花数" ?

    本程序转载自:如何得到所有的水仙花数

    感谢Android_iPhone日知己所无),preferme冰思雨)等人;


    package test;
    
    import java.math.BigInteger;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * 找出所有的水仙花数
     * 判断一个数是否为水仙花数:一个N位整数,其各位数字的N次方的和等于该数本身
     *
     * @author preferme (冰思雨)
     *
     */
    public class ShuiXianHuaTree {
    
    	public static final SimpleDateFormat SDF = new SimpleDateFormat(
    			"yyyy-MM--dd HH:mm:ss.SSS");
    	public static final BigInteger EndPoint = new BigInteger(
    			"115132219018763992565095597973971522402");
    	private static final BigInteger Poison = new BigInteger("-1");
    
    	private static class CompareThread extends Thread {
    		private BlockingQueue<BigInteger> numbers;
    
    		public CompareThread(BlockingQueue<BigInteger> queue) {
    			numbers = queue;
    		}
    
    		public void run() {
    			BigInteger number = BigInteger.ZERO;
    			try {
    				while ((number = numbers.take()) != Poison) {
    					if (isNarcissisticNumber(number)) {
    						System.out.println(SDF.format(new Date()) + "	"
    								+ number);
    					}
    				}
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private static final BigInteger[][] Powers = new BigInteger[40][10];
    	static {
    		for (int i = 0; i < 40; i++) {
    			for (int j = 0; j < 10; j++) {
    				Powers[i][j] = BigInteger.valueOf(j).pow(i);
    			}
    		}
    	}
    
    	public static boolean isNarcissisticNumber(final BigInteger number) {
    		BigInteger sum = BigInteger.ZERO; // 各位数字的N次方的和
    		char[] digitArray = number.toString(10).toCharArray(); // 各位数字的数组
    		for (char digit : digitArray) {
    			sum = sum.add(Powers[digitArray.length][digit - '0']);
    		}
    		return sum.compareTo(number) == 0;
    	}
    
    	public static void main(String[] args) throws InterruptedException {
    		BlockingQueue<BigInteger> queue = new LinkedBlockingQueue<BigInteger>(
    				1000);
    		CompareThread[] pool = new CompareThread[Runtime.getRuntime()
    				.availableProcessors()];
    		for (int i = 0; i < pool.length; i++) {
    			pool[i] = new CompareThread(queue);
    			pool[i].setPriority(Thread.MIN_PRIORITY);
    			pool[i].start();
    		}
    		System.out.println("水仙花数列表");
    		for (BigInteger number = BigInteger.ZERO; number.compareTo(EndPoint) <= 0; number = number
    				.add(BigInteger.ONE)) {
    			queue.put(number);
    		}
    	}
    }
    


  • 相关阅读:
    Rails http://poj.org/problem?id=1363
    表达式求值 http://acm.nyist.net/JudgeOnline/problem.php?pid=305
    精 挑 细 选 http://acm.nyist.net/JudgeOnline/problem.php?pid=263
    荷兰国旗问题 http://acm.nyist.net/JudgeOnline/problem.php?pid=268
    16进制的简单运算http://acm.nyist.net/JudgeOnline/problem.php?pid=244
    新浪博客中特殊字符不显示的问题
    C语言注释技巧
    c语言utf8转unicode
    [Linux]使用cat向同一个文件中写入多行
    大数据时代的数据价值_hadoop视频教程精品推荐
  • 原文地址:https://www.cnblogs.com/xiaozhang2014/p/5297283.html
Copyright © 2011-2022 走看看