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

java程序代码如下:

public class ArithTest {
    public static void main(String[] args) {
        ArithTest at = new ArithTest();
        //打印所有的水仙花数
        System.out.println("水仙花数:");
        for(int a=100;a<=999;a++){
            boolean bl = at.fk(a);
            if(bl){
                System.out.println(a);
            }
        }
    /* 水仙花数
     * 所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。
     * 打印所有水仙花数
     */
    public boolean fk(int a){
        int x = a/100;
        int y = a%100/10;
        int z = a%10;
        int k = x*x*x+y*y*y+z*z*z;
        if(a == k){
            return true;
        } else {
            return false;
        }
    }
}

执行结果如下:

水仙花数:
153
370
371
407

 

相关文章:

  • 2021-08-13
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案