黑马程序员技术交流社区

标题: 输出所有的水仙花谁 [打印本页]

作者: BugBuff    时间: 2016-3-26 22:55
标题: 输出所有的水仙花谁
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 + "个水仙数");
        }
}
作者: 伊乐杰    时间: 2016-3-26 23:44
9996翻了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2