100——1000之间的水仙花数?并统计有多少个水仙花数?

100——1000之间的水仙花数?并统计有多少个水仙花数?

水仙花数的概念

所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
举例:153就是一个水仙花数。
153 = 1*1*1 + 5*5*5 + 3*3*3

计算100——1000之间的水仙花数?并统计有多少个水仙花数?

/*
    计算100——1000之间的水仙花数?并统计有多少个水仙花数?
 */
public class OptWhileDemo1 {
    public static void main(String[] args) {
        int i = 100;
        int num = 0;//用于计数
        while (i < 1000) {
            int g = i % 10;
            int s = i / 10 % 10;
            int b = i / 100 % 10;
            if (i == (Math.pow(g, 3) + Math.pow(s, 3) + Math.pow(b, 3))) {
                System.out.print(i + "\t");
                num++;//找到水仙花数自加1
            }
            i++;
        }
        System.out.println();
        System.out.println("100-1000之间的水仙花数有" + num + "个");
    }
}

运行截图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值