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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 聂益飞   /  2012-5-16 21:26  /  3209 人查看  /  26 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

于陈 发表于 2012-5-16 22:03
楼上的输出有点问题啊~
输出的是:
x=0,y=0,z=0

还是你的想法和我一开始的想法最接近
回复 使用道具 举报
其实这个题的目的是取出个十百位的数字就可以了 不难的 嘿嘿
/*输出水仙花数字abc=a*a*a+b*b*b+c*c*c
*
* */
public class WaterFlower {

        public static void main(String[] args) {
               
                int count=0;                                 //水仙花个数统计
                System.out.println("100-1000之间的水仙花:");
                for(int i=100;i<1000;i++){
                       
                        int g=(i%100)%10;                //g代表个位数
                       
                        int s=(i%100-g)/10;     //s代表十位数
                       
                        int b=(i-s*10-g)/100;   //b代表百位数
                       
                       
                        if(i==(g*g*g+s*s*s+b*b*b)){
                               
                                count++;
                               
                                System.out.println("水仙花"+count+"-->"+i);
                        }
                }
        }
}
结果:
100-1000之间的水仙花:
水仙花1-->153
水仙花2-->370
水仙花3-->371
水仙花4-->407
回复 使用道具 举报
本帖最后由 王亚男 于 2012-5-17 01:39 编辑
于陈 发表于 2012-5-16 22:03
楼上的输出有点问题啊~
输出的是:
x=0,y=0,z=0

哈哈~,百位从1开始取不就行了~还少了一步判断,提高了效率~~
回复 使用道具 举报
本帖最后由 了无尘 于 2012-5-17 05:40 编辑

很多年前就做过这个了
这破缩进搞不定了,就这么地吧。。。。。我崩溃了,我投降
                //显示n位的水仙花数,4就是显示4位数的水仙花数
                //n>=3 小于3位的数不是水仙花数
                int n = 5;
                for (int i = (int)Math.pow(10, n-1); i < Math.pow(10, n); i++)
               {
                           int[] bits = new int[n];
                           int tmp = i;
                           int sum = 0;
                           for (int j = bits.length - 1; j > ~0; j--, tmp/=10)
                        {
                    
                               sum += Math.pow((bits[j] = tmp % 10), n);
                        }
                            if(i == sum)
                           {
                                   System.out.println(i);
                           }
                    }
回复 使用道具 举报
//这样应该能行
package com.study;

public class TestFlower {
        public static void main(String[] args) {
                int temp = 0,i1,i2,i3;
                for(int i=100;i<=999;i++) {
                        i1=i%10;
                        i2=(i%100)/10;
                        i3=i/100;
                        temp=(int) (Math.pow(i1,3)+Math.pow(i2,3)+Math.pow(i3,3));
                        if(i==temp){
                                System.out.println(temp);
                        }
                }
        }

}
回复 使用道具 举报
  1. public class Flowers {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 for(int x=100;x<1000;x++){
  8.                         if((x%10)*(x%10)*(x%10)+(x%100/10)*(x%100/10)*(x%100/10)+(x%1000/100)*(x%1000/100)*(x%1000/100)==x)
  9.                                 System.out.println(x);
  10.                 }
  11.                
  12.                 int t=1;               
  13.                 while(true){
  14.                        
  15.                         int sum=0;
  16.                         for(int q=t;q!=0;q=q/10){               
  17.                                 sum+=(q%10)*(q%10)*(q%10);
  18.                         }
  19.                         if(sum==t)
  20.                                 System.out.println(sum);                               
  21.                         t++;
  22.                                        
  23.                 }                                       
  24.                                        
  25.         }

  26. }
复制代码
回复 使用道具 举报
黄秋 黑马帝 2012-5-18 20:49:00
27#
看到大家这么踊跃,我也来凑凑热闹:
  1. class Test {
  2.         public static void main(String[] args) {
  3.                 for(int i=100;i<1000;i++){
  4.                         int sum=0;
  5.                         for(int j=0;j<3;j++){
  6.                                 int temp=Integer.parseInt((i+"").charAt(j)+"");
  7.                                 sum+=Math.pow(temp, 3);
  8.                         }
  9.                         if(i==sum)        System.out.println(i);
  10.                 }
  11.         }
  12. }
复制代码
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马