输出所有的水仙花数

输出所有的水仙花数
水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)
一个三位数i,取十位上的数字的方法有两种:
1. i%100/10
2. i/10%10

/*
 * 输出所有的水仙花数
 * v1.0
 */

package com.hongyewell.com;

public class Flower {

    public static void main(String[] args){
        int i;
        for(i=100;i<1000;i++){
            int a = i%10;   //个位
            int b = i%100/10;  //十位  
            int c = i/100;  //百位
            if(a*a*a+b*b*b+c*c*c==i){
                System.out.println("水仙花数:"+i);
            }
        }
    }

}

/*
 * 输出所有的水仙花数
 * v2.0
 * Math.pow(x,y) 计算x的y次方
 */

package com.hongyewell.com;

public class Flower {

    public static void main(String[] args){
        int i;
        for(i=100;i<1000;i++){
            int a = i%10;   //个位
            int b = i%100/10;  //十位  
            int c = i/100;  //百位
            if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3)==i){
                System.out.println("水仙花数:"+i);
            }
        }
    }

}

/*
 * 输出所有的水仙花数
 * v3.0
 * Math.pow(x,y) 计算x的y次方
 * Math.pow() 返回结果为double型
 * 和int 类型比较的时候 最好强转一下
 */

package com.hongyewell.com;

public class Flower {

    public static void main(String[] args){
        int i;
        for(i=100;i<1000;i++){
            int a = i%10;   //个位
            int b = i%100/10;  //十位  
            int c = i/100;  //百位
            int j = (int) (Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3)); 
            if(j==i){
                System.out.println("水仙花数:"+i);

            }
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值