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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
        打印出所有的"水仙花数"。
                                所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
                                153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
                                提示:
                                1:采用循环取得所有的三位数。(三位数指的是100-999之间的数)。
                                2:把每个三位数的个位,十位,百位进行分解。

*/
class ForTest
{
        public static void main(String[] args)
        {
               
                for (int x=999;x>100;x--)
                {
                       
                       
                        int a=x/100;
                        int b=(x-x*100)/10;
                        int c=x-a*100-b*10;
                        if (x==a*a*a+b*b*b+c*c*c)
                        {
                                System.out.println(x);
                        }
                       
                       

                }                       
       
        }
}

9 个回复

倒序浏览
             class Demo
{
     public static void main(String[] args)
{
               int count = 0;
                for(int num = 100 ; num <= 999 ; num++){
                        //取每一位
                        int ge = num % 10;
                        int shi = num / 10 % 10;
                        int bai = num  / 100 % 10;

                        //判断
                        if((ge * ge * ge + shi * shi * shi + bai * bai * bai) == num){
                                System.out.println("找到水仙花数:" + num);
                                count++;
                        }
                }
                System.out.println("水仙花的数量是:" + count);
        }
}
回复 使用道具 举报 1 0
阿康啊,你这头像直接无敌啊
回复 使用道具 举报
你除法之后应该取模。
回复 使用道具 举报
for循环语句错误  1L已经把代码贴过来了  个位 十位 百位的标准取法 就是这样
回复 使用道具 举报
int b = (x-a*100)/10 ,不是(x-x*100)/10吧
回复 使用道具 举报
gaoer 中级黑马 2015-5-25 23:34:55
7#
来瞧瞧 。~~~~~~~~~~~~!
回复 使用道具 举报
小蒙 中级黑马 2015-5-25 23:40:38
8#
class FlowerDemo
{
        public static void main(String[] args)
        {
                for(int x=100; x<1000; x++)
                {
                        //x就是任意数据

                        int ge = x%10;                        //        用x取余得到各位上的数
                        int shi = x/10%10;                //   x除10并取余得到十位上的数               
                        int bai = x/10/10%10;   //   X除100并取余得到百位上的数

                        if((ge*ge*ge+shi*shi*shi+bai*bai*bai)==x)
                        {
                                System.out.println(x);
                        }
                }
        }
}
回复 使用道具 举报
ok,看到错误了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马