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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王际涛 中级黑马   /  2015-11-4 23:28  /  2648 人查看  /  27 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

数越大水仙花数出现的越少
import java.util.Scanner;
class shuixianhua2 {
        public static void main(String[] args) {
                Scanner in=new Scanner(System.in);
                while(true){
                        sop("输入水仙花数值上限:(如:1000或10000或更大的数)");
                        long asd=in.nextInt();
                        sop("从100到"+asd+"之间的水仙花数是:");
                        shuixianhua(asd);
                }       
        }       
        public static void shuixianhua(long a){//循环判断水仙花数
                int count=0;
                for (long i=100;i<a ;i++ ) {
                        if (fenhe(i)==i) {
                                sop(i);
                                count++;
                        }
                }
                sop("总个数是"+count);
        }
        public static long fenhe(long a){//n个数字中每个数字n次幂的和
                long sum=0;
                int geshu1 = geshu(a);
                int[] asd=new int[geshu1];
                int q=0;
                while(a>0){
                        asd[q]=(int)(a%10);
                        a/=10;
                        q++;
                }
                for(int j=0;j<geshu1;j++){
                        sum+=mi(asd[j],geshu1);
                }
                return sum;
        }

        public static int geshu(long a){//输入数字的个数
                        int count1=0;
                        long a1=a;
               
                        while (a1>0) {
                                a1/=10;
                                count1++;
                        }
                        return count1;
        }
        public static int mi(int a,int b){  //
                int mii=1;
                for (int i=0; i<b;i++ ) {
                        mii*=a;
                }
                return mii;
        }
       
         public static void sop(Object a){
                System.out.println(a);
        }
}

27 个回复

倒序浏览
哎,写的乱七八糟的…
回复 使用道具 举报
小伙子 ,不错啊.
回复 使用道具 举报
好歹写点注释呗
回复 使用道具 举报
6666666666666666666
回复 使用道具 举报
水仙花数,不是就4个么.三位数的
回复 使用道具 举报
jlq 中级黑马 2015-11-5 13:40:23
7#
soyidesinei
回复 使用道具 举报
水仙花数,这个不是应该多加些注释么?
回复 使用道具 举报
看完之后我醉了
回复 使用道具 举报
看得眼晕
回复 使用道具 举报
这代码看得 。。。。
回复 使用道具 举报
  1.                 int a,b,c,sum ;
  2.                 StringBuilder sb = new StringBuilder();
  3.                 for (int i =100;i<1000;i++){
  4.                         sb.append(i);
  5.                         a = sb.charAt(0)-48;
  6.                         b = sb.charAt(1)-48;
  7.                         c = sb.charAt(2)-48;
  8.                         sum = a*a*a+b*b*b+c*c*c;
  9.                         if(sum == i){
  10.                                 System.out.println(i);
  11.                         }
  12.                         sb.delete(0,sb.length());

  13.                 }
复制代码
回复 使用道具 举报
scanner 这个对象不是很熟,我想问楼主这个运行后是不是你输入一个数字然后按一下回车就ok了?
这个in.nextInt()相当于那种等待式的方法么,就一直在那等着用户去输入值。
是么?
回复 使用道具 举报
水仙花数是三位数啊
回复 使用道具 举报
水仙花数不应该是三位数么 ?
回复 使用道具 举报
478883662 发表于 2015-11-5 18:45
scanner 这个对象不是很熟,我想问楼主这个运行后是不是你输入一个数字然后按一下回车就ok了?
这个in.next ...

输入一个数字然后按一下回车就ok了
回复 使用道具 举报
zyh1578814923 发表于 2015-11-5 20:31
水仙花数是三位数啊

三位数的数字三次方和,四位数的数字四次方和,五位数的数字五次方和……然后,就这样了,纯属逗自己玩
回复 使用道具 举报
Camwly 中级黑马 2015-11-24 23:58:16
18#
好东西啊
回复 使用道具 举报
anuo 中级黑马 2015-11-26 09:45:59
19#
写作习惯希望可以改改哦,别人看的真心累
回复 使用道具 举报
简化 简化 !!!!!!!!!!!!!!!!1
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马