标签:package search color math sea ima 三次 system 立方和
package org.jimmy.autosearch.test; public class NarcissusNumberTest2019123101 { public static void main(String[] args) { try { test(); } catch (Exception e) { e.printStackTrace(); } } public static void test() throws Exception{ /*打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。 例如: 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。*/ int firstNumber = 0; int secondNumber = 0; int thirdNumber = 0; int power = 3; for(int i = 100; i < 1000; i++){ thirdNumber = i / 100; secondNumber = i % 100 / 10; firstNumber = i % 10; if(Math.pow(firstNumber, power) + Math.pow(secondNumber, power) + Math.pow(thirdNumber, power) == i){ System.out.println("水仙花数:" + i); } } } }
运行结果:
标签:package search color math sea ima 三次 system 立方和
原文地址:https://www.cnblogs.com/JimmySeraph/p/12123480.html