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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 董晗 中级黑马   /  2014-12-14 00:03  /  1909 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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




13 个回复

倒序浏览
class Hua
{
        public static void main(String[] args)
        {
                for(int a=100; a<=999;a++)//定义a为要求的三位数
                {
                        int x=a/100%10;//表示出百位数
                        int y=a/10%10;//表示出十位数
                        int z=a%10;//个位
                        if(a==x*x*x+y*y*y+z*z*z)//水仙花的定义
                        {
                                System.out.println(a);
                        }
                }
        }
}
回复 使用道具 举报
清浅。。 发表于 2014-12-14 00:15
class Hua
{
        public static void main(String[] args)

看我注释的位数求法,以此类推。。。。
回复 使用道具 举报
class Demo1
{
       public static void main(String[ ] args)
       {
                   for (int x=100;x<=999 ;x++ )
                   {
                           int bai=x/100;
                           int shi=(x-num*100)/10;
                           int ge=(x-num*100-z*10);
                                   int b=bai*bai*bai+shi*shi*shi+ge*ge*ge;
                                   if (b==x)
                                   {
                                           System.out.println("b="+b);
                                   }
                   }
           }
               
}
输出的就是水仙花
回复 使用道具 举报
class ShuiXianHuaShu{
        public static void main(String[] args) {;
                for(int i=100;i<=1000;i++){
                        int n=i;
                        int m=n;
                        int sum =0;
                        while(n!=0){
                                int last=n%10;
                                sum =sum+last*last*last;
                                n/=10;
                        }
                        if(sum==m){
                                System.out.println(m+"是水仙花数!");
                        }
                }
        }
}
这个绝对没问题!

捕获.PNG (123.18 KB, 下载次数: 69)

捕获.PNG
回复 使用道具 举报
                        int bai=a/100%10;//    表示出百位数
                        int shi=a/10%10;//     表示出十位数
                        int ge=a%10;//      个位
回复 使用道具 举报
  1. /*
  2.         需求:在控制台输出所有的“水仙花数”
  3.         分析:
  4.         水仙花数:指一个三位数,其各位数字的立方和等于该数本身。
  5.         1、确定范围:100 ~ 999
  6.         2、获取三位数的个、十、百上的数字。
  7.                 以153为例:
  8.                 ge: 153%10 = 3
  9.                 shi: 153/10%10 = 5;
  10.                 bai: 153/10/10%10 = 1;
  11. */
  12. class WaterFlower
  13. {
  14.         public static void main(String[] args)
  15.         {
  16.                 //遍历取数范围
  17.                 for(int x = 100; x<1000; x++)
  18.                 {
  19.                         //个位
  20.                         int u = x%10;
  21.                         //十位
  22.                         int t = x/10%10;
  23.                         //百位
  24.                         int h = x/10/10%10;
  25.                         if(x == (u*u*u + t*t*t+ h*h*h))
  26.                         {
  27.                                 //如果相同,就把该数输出到控制台
  28.                                 System.out.println("x="+x);
  29.                         }
  30.                 }
  31.         }
  32. }
复制代码
回复 使用道具 举报
二位以上的数有没有可能满足水仙花数的条件,应该叫什么数?
回复 使用道具 举报
来向各位大神学习下!
回复 使用道具 举报
亲,看前两天的贴子吧,都有
回复 使用道具 举报
董晗 中级黑马 2014-12-14 15:59:50
11#
清浅。。 发表于 2014-12-14 00:15
class Hua
{
        public static void main(String[] args)

嗯,取余,谢谢哈
回复 使用道具 举报
董晗 中级黑马 2014-12-14 16:02:47
12#
魏文杰 发表于 2014-12-14 00:40
class ShuiXianHuaShu{
        public static void main(String[] args) {;
                for(int i=100;i

好了,弄出来了谢谢哈
回复 使用道具 举报
董晗 中级黑马 2014-12-14 16:04:16
13#
擒贼先擒王 发表于 2014-12-14 08:46
二位以上的数有没有可能满足水仙花数的条件,应该叫什么数?

不知道啊。。。
回复 使用道具 举报
董晗 发表于 2014-12-14 16:02
好了,弄出来了谢谢哈

不客气。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马