1 package test_1_3; 2 3 public class NarcissisticNumber { 4 5 public static void main(String[] args) { 6 7 /** 打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。*/ 8 9 System.out.println("水仙花数有:"); 10 for (int i = 100; i < 1000; i++) { 11 if (isNracNum(i) == true) { 12 System.out.print(i + " "); 13 } 14 } 15 16 } 17 18 private static boolean isNracNum(int num) { 19 20 int num1 = num % 10; 21 int num2 = num / 10 % 10; 22 int num3 = num / 100; 23 24 if (Math.pow(num1, 3) + Math.pow(num2, 3) + Math.pow(num3, 3) == num) { 25 return true; 26 } 27 28 return false; 29 } 30 31 }
结果如下:
水仙花数有:
153 370 371 407
[20-04-26][Self-test 3]Java Narcissistic Number
原文:https://www.cnblogs.com/mirai3usi9/p/12781502.html