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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王飞 中级黑马   /  2012-7-21 22:26  /  2384 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王飞 于 2012-7-22 09:01 编辑

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

                        1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
         */


我的水仙数也符合上面的规则,这为什么不是和你们的水仙数一样呢?
难道我这就不是水仙数了吗?
求解释啊,做了半天怎么和你们的水仙数不一样啊
{:soso_e101:}



public class aa {
        

    public static void main(String[] args)
    {
            String s = null;
            
    //下面的三个for表示三个位数,第一位不能为零,后面两位可以为零。
            
            for(int a = 1;a<10;a++)   //第一位
            {
                    for(int b = 0;b<10;b++) //第二位
                {
                            for(int c = 0;c<10;c++)  //第三位
                        {
                                s =""+a+b+c;   //把三位数的组合变成字符串
                                if(((int)Math.pow(a, 3)+""+(int)Math.pow(b, 3)+(int)Math.pow(c, 3)).equals(s))
                                {
                                        //这里判断的是a的三次+b的三次+c的三次的字符串如果是s(a+b+c的字符串),那么就符合条件即:其各位数字立方和等于该数本身。
                                        System.out.println(s);
                                }
                        }
                }
            }
    }
            
}

我的水仙数是:
100
110
111




未命名.jpg (7.41 KB, 下载次数: 20)

我的水仙数

我的水仙数

10 个回复

倒序浏览
if(((int)Math.pow(a, 3)+""+(int)Math.pow(b, 3)+(int)Math.pow(c, 3)).equals(s))
多了个""   你这样不是把3个数的和相加是把a,b,c的3次方连接起来
回复 使用道具 举报
应该是这样  if(((int)Math.pow(a, 3)+(int)Math.pow(b, 3)+(int)Math.pow(c, 3)+"").equals(s))
回复 使用道具 举报
建议楼主用eclipse的debug调试下程序,你会明白为什么错的,还有没必要写那么复杂吧,这个原理很简单的
回复 使用道具 举报
  1. class  Test1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int num = 100,count = 0;
  6.                 for(int x=100;x<1000;x++)
  7.                 {
  8.                        
  9.                         if(Math.pow(num/100,3)+Math.pow(num/10%10,3)+Math.pow(num%10,3)==num)
  10.                         {
  11.                                 System.out.println(num);
  12.                                 count++;
  13.                         }
  14.                         num++;
  15.                 }
  16.                 System.out.println("count="+count);
  17.        
  18.         }
  19. }
复制代码
看看我这个,容易理解点。
回复 使用道具 举报
  1. class  Test1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int num = 100,count = 0;
  6.                 for(int x=100;x<1000;x++)
  7.                 {
  8.                         
  9.                         if(Math.pow(num/100,3)+Math.pow(num/10%10,3)+Math.pow(num%10,3)==num)
  10.                         {
  11.                                 System.out.println(num);
  12.                                 count++;
  13.                         }
  14.                         num++;
  15.                 }
  16.                 System.out.println("count="+count);
  17.         
  18.         }
  19. }
复制代码
看看我这个,容易理解点。

1.png (2.83 KB, 下载次数: 30)

1.png

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 水木桶 于 2012-7-22 07:51 编辑
  1. package com.learn;

  2. public class Shuixian {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args)
  7.     {
  8.                 String s = null;
  9.                
  10.                 //下面的三个for表示三个位数,第一位不能为零,后面两位可以为零。
  11.                      
  12.                     for(int a = 1;a<10;a++)   //第一位
  13.                      {
  14.                              for(int b = 0;b<10;b++) //第二位
  15.                          {
  16.                                      for(int c = 0;c<10;c++)  //第三位
  17.                                  {
  18.                                          s =""+a+b+c;   //把三位数的组合变成字符串
  19.                                          int temp = ((int)Math.pow(a, 3)+(int)Math.pow(b, 3)+(int)Math.pow(c, 3));//改成这样。。
  20.                                          if((temp+"").equals(s))  //这里判断。。
  21.                                         {
  22.                                                 
  23.                                                  System.out.println(s);
  24.                                          }
  25.                                  }
  26.                          }
  27.                      }
  28.              }
  29.                      
  30.          }
  31.          

复制代码
运行结果


应该是整型与字符一起运算,影响了3次方运算。

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
看看 我的吧 更容易理解
  1. public class Shuixianshu {

  2.         public static void main(String[] args) {
  3.                 int a,b,c;
  4.        for(int i=100;i<1000;i++){
  5.                 a=i/100;
  6.                 b=(i/10)%10;
  7.                 c=i%10;
  8.                 if(((a*a*a)+(b*b*b)+(c*c*c))==i){
  9.                         System.out.println(i+"是水仙花数");
  10.                        
  11.                 }
  12.         }

  13. }}
复制代码
C:\Users\CPF\Desktop

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
蔡鹏飞 发表于 2012-7-22 08:28
看看 我的吧 更容易理解

运行结果

未命名.jpg (6.75 KB, 下载次数: 28)

未命名.jpg
回复 使用道具 举报
看来我又来晚了一步
回复 使用道具 举报

public class daffodilDemo {
        public static void main(String[] args){
                for(int x=100;x<1000;x++){
                        if(Math.pow(x/100,3)+(Math.pow(x/10%10,3))+(Math.pow(x%10,3))==x)
                                System.out.println(x);
                }
        }

}
我觉得这样更好
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马