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

感谢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()) + "\t"
+ 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);
}
}
}

(7)如何得到所有的 "水仙花数" ?的更多相关文章

  1. C语言 · 4-3水仙花数

    问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 153=1^3+5^3+3^3. 样例输入 一个满足题目要求的输入范例.例:无 样例输出 ...

  2. Java程序设计之打印100~999的水仙花数

    package printDaffodilNumber; /* * 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身 ...

  3. 51Nod--1015 水仙花数

    51Nod:  http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1015   1015 水仙花数 基准时间限制:1 秒 空间 ...

  4. js查找水仙花数

    所谓水仙花数是满足类似于153=1³+5³+3³: 第一种方式:把这个数当做字符串来实现 <script> for(var i=100;i<=999;i++) { str_i=i.t ...

  5. java 实现(代码) -- 水仙花数 + 杨辉三角形

    /* 在控制台输出所有的“水仙花数” 水仙花:100-999 在以上数字范围内:这个数=个位*个位*个位+十位*十位*十位+百位*百位*百位 例如:xyz=x^3 +y^3 +z^3 怎么把三位数字拆 ...

  6. 编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print

    package zuoye; public class print { void output() { System.out.println("100-999之间的水仙花数是:") ...

  7. hdu 2010 - 水仙花数

    题意: 数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求输出所有在m和n范围内的水仙花 ...

  8. C语言与水仙花数

    C语言与水仙花数 水仙花数:前提三位数,"个位数的立方"加上"十位数的立方"加上"百位数的立方"恰好等于这个数. 我们来用C语言书写水仙花数 ...

  9. 题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花 数 ",因为153=1的三次方+5的三次方+3的三次方。

    题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个 "水仙花 数 ", ...

  10. python练习——水仙花数

    题目: 请判断一个数是不是水仙花数.其中水仙花数定义各个位数立方和等于它本身的三位数.输入有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)输入0表示程序输入结束.输出 ...

随机推荐

  1. java与数据结构(6)---java实现链栈

    栈之链式存储结构链栈 链栈 栈的链式存储结构成为链栈.链栈是没有头结点,头结点就是栈顶指针top. 代码结构 package list; public interface Stackable;公共接口 ...

  2. vimrc 留备份

    set encoding=UTF-8 "encode with UTF-8"set backspace=2set nusyn onset ai!syntax enablesynta ...

  3. CROW-5 WEB APP引擎商业计划书(HTML5方向)-微信网页版微信公众平台登录-水仙谷

    CROW-5 WEB APP引擎商业计划书(HTML5方向)-微信网页版微信公众平台登录-水仙谷 CROW-5 WEB APP引擎商业计划书(HTML5方向)

  4. iOS--创建uiscrollview

    //创建uiscrollview self.PageHeight = self.view.bounds.size.height; self.PageWidth = self.view.bounds.s ...

  5. NuGet 问题及小技巧

    在使用NuGet程序包管理时,经常会遇到的一些小问题.比如,联网搜索时间太长,搜索不到常见或想要的程序包,程序包版本兼容问题,想安装制定的版本等. 那么我们可以使用以下的一些小技巧来解决. 1.检查N ...

  6. qemu-img 快照的一些总结

    qemu-img 快照的一些总结 http://www.openext.org/2014/06/qemu-img-snapshot-re http://blog.csdn.net/muge0913/a ...

  7. 判断是否是IP地址

    static bool IsIP(QString IP) { QRegExp RegExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\ ...

  8. angularJS环境安装

    第一步: 安装node.js,进入node.js官网(http://nodejs.org/)下载安装相应的node.js版本:

  9. (转)在Repeater中嵌套使用Repeater

    在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品.最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加 ...

  10. arcmap从excel坐标数据生成点shp文件

    概述 今天试图在ArcMap中将excel数据转成点文件,在"Display XY Data"的时候,无法选择X,Y字段,很是纳闷,原来Excel中列的格式是文本,导致无法选择.有 ...