水仙花数问题

xiaoxiao2021-02-28  110

很早之前面试时碰到过这个题目,现在总结一下。

水仙花数是指一个 n 位正整数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)

代码如下:

public class Flower { /** * @return 100~999之内的水仙花数 */ public static void main(String[] args) { for(int a = 100;a<1000;a++){ int bit = a; int ten = a0/10; int hundred = a/100; int s = (int) (Math.pow(bit, 3)+Math.pow(ten, 3)+Math.pow(hundred, 3)); if(a == s){ System.out.println(bit+"***"+ten+"***"+hundred); System.out.println(s); } } } }

转载请注明原文地址: https://www.6miu.com/read-55249.html

最新回复(0)