Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:有简单一点的求水仙花数的方法吗?? [Re:Jasmin]
cowskin





发贴: 1
积分: 0
于 2006-01-15 11:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵,小弟刚刚学了这个,这个是我老师写的程序,很牛哦~

public class Suixian {
  public static void main(String[] args) throws java.io.IOException {
  
    byte[] buf = new byte[20];
    int cmdLength = System.in.read(buf);
    String str = new String(buf,0,cmdLength-2);
    int n = Integer.parseInt(str);//这里当然是输入一个位数罗,也不一定就三位吧
    
    int low = 1,high = 1;
    for (int i=1;i<n;i++) low = low*10;
    high = low * 10;
    //System.out.println(low);
    //System.out.println(high);
    for (int i=low;i<high;i++)
    {
      int sum = 0;
      int p = i;
      while (p!=0)
      {
        int r = p%10;
        p = p/10;
        int rn = 1;
        for (int j=1;j<=n;j++) rn = rn * r;
        sum = sum + rn;
      }
      
      if (sum==i) System.out.printlnLight Bulb;
      
    }
    
  }  
}
不过这只是你程序的一个化简,(你的用了一个数组)
第二个方法是这样,拿三未数举例

public class SuiXian2 {
  
  
  public static void main(String[] args) {
    for (int a=1;a<=9;a++)
      for (int b=0;b<=9;b++)
        for (int c=0;c<=9;c++)
          if(a*a*a+b*b*b+c*c*c==100*a+10*b+c)
            System.out.println(100*a+10*b+c);
  }  
}
这是我上论坛来发的第一封贴子,望指教




话题树型展开
人气 标题 作者 字数 发贴时间
7956 有简单一点的求水仙花数的方法吗?? Jasmin 849 2006-01-08 18:48
6899 Re:有简单一点的求水仙花数的方法吗?? lianfuye 331 2006-01-11 17:11
6397 Re:有简单一点的求水仙花数的方法吗?? awardwy 551 2006-01-12 11:38
6664 Re:有简单一点的求水仙花数的方法吗?? cowskin 2148 2006-01-15 11:23

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923