A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© BugBuff 中级黑马   /  2016-3-26 22:55  /  865 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.lang.Math;
class Test_Narcissus  {
        /** A:案例演示
        * 需求:在控制台输出所有的”水仙花数”

        * 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
        * 举例:153就是一个水仙花数。
        * 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
        */
        public static void main(String[] args)  {
                int hundred,ten,ind,pos = 0;
                for (int i = 100;i < 1000;i++) {
                        hundred = (int)Math.pow(i / 100,3);
                        ten = (int)Math.pow((i % 100) /10,3);
                        ind = (int)Math.pow((i % 10),3);
                        if((hundred + ten + ind) == i) {
                                pos++;
                                System.out.print(i + " ");
                        }
                }
                System.out.println();
                System.out.println("一共有" + pos + "个水仙数");
        }
}

1 个回复

倒序浏览
9996翻了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马