输出所有的水仙花数

需求:输出所有的”水仙花数”

  • 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。

  • 举例:153就是一个水仙花数。
  • 153 = 111 + 555 + 333 = 1 + 125 + 27 = 153
class Hello2 {
    public static void main(String[] args) {
for (int i = 100;i <= 999 ;i++)
        {
             int ge = i % 10;
             int shi = i / 10 % 10;
             int bai = i / 10 / 10 % 10;
             if (i == ge * ge * ge + shi * shi *shi + bai * bai * bai)
             {
                 System.out.println(i);
             }
        }        
    }
}

结果:

原文地址:https://www.cnblogs.com/Wangzui1127/p/11160429.html