[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
[size=1em]37
[size=1em]38
[size=1em]39
[size=1em]40
[size=1em]41
[size=1em]42
[size=1em]43
[size=1em]44
[size=1em]45
[size=1em]46
[size=1em]47
[size=1em]48
[size=1em]49
[size=1em]50
| [size=1em][size=1em]import java.math.*;
[size=1em]import java.util.*;
[size=1em]public class NarcissisticNumber {
[size=1em] public static void main(final String[] args) {
[size=1em] System.out.println("水仙花数列表");
[size=1em] for (BigInteger bigInteger = new BigInteger("0");
[size=1em] !bigInteger.equals(new BigInteger("115132219018763992565095597973971522402"));
[size=1em] bigInteger = bigInteger.add(new BigInteger("1"))) {
[size=1em] if (isNarcissisticNumber(bigInteger)) {
[size=1em] System.out.println(getDateTime() + "\t" + bigInteger);
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em] /**
[size=1em] * 判断一个数是否为水仙花数:一个N位整数,其各位数字的N次方的和等于该数本身
[size=1em] *
[size=1em] * @param number
[size=1em] * @return 当输入的参数为水仙花数时返回true,否则返回false
[size=1em] */
[size=1em] public static boolean isNarcissisticNumber(final BigInteger number) {
[size=1em] BigInteger sumOfDigitPower = new BigInteger("0"); // 各位数字的N次方的和
[size=1em] char[] digitArray = number.toString().toCharArray(); // 各位数字的数组
[size=1em] for (char digit : digitArray) {
[size=1em] sumOfDigitPower = sumOfDigitPower.add( // 求和
[size=1em] BigInteger.valueOf(
[size=1em] Character.digit(digit, 10) // 各位数字
[size=1em] ).pow(digitArray.length) // N次方
[size=1em] );
[size=1em] }
[size=1em] return sumOfDigitPower.equals(number);
[size=1em] }
[size=1em] /**
[size=1em] * @return 返回包含当前日期和时间的字符串
[size=1em] */
[size=1em] public static String getDateTime() {
[size=1em] Calendar calendar = Calendar.getInstance();
[size=1em] return ""
[size=1em] + calendar.get(Calendar.YEAR) + "/"
[size=1em] + (calendar.get(Calendar.MONTH) + 1) + "/"
[size=1em] + calendar.get(Calendar.DATE) + " "
[size=1em] + calendar.get(Calendar.HOUR_OF_DAY) + ":"
[size=1em] + calendar.get(Calendar.MINUTE) + ":"
[size=1em] + calendar.get(Calendar.SECOND) + ":"
[size=1em] + calendar.get(Calendar.MILLISECOND);
[size=1em] }
[size=1em]}
|