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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ╄Tracyぺ 中级黑马   /  2014-7-17 19:19  /  1627 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
我求出的只有3个:6、28、496,可是答案有24,到底24是不是。。。如果24算的话,也就是并没有算上24所有的因子之和,求大神指点

5 个回复

倒序浏览
通过下面的小程序打印出的完数是4  28  496    所以24并不是完数
  1. public static void method1()
  2.         {
  3.                 int sum = 0;//定义和存放的变量
  4.                 for(int i=1;i<=1000;i++)//在1-1000内循环
  5.                 {
  6.                         sum = 0;
  7.                         for(int j=1;j<i-1;j++)//遍历比i小的每个数,找出其因子
  8.                         {
  9.                                 if(i%j==0)//找因子
  10.                                         sum = sum + j;//因子的和

  11.                         }
  12.                         if(i==sum)//若因子和与该数相同,则是完数
  13.                                 System.out.println("完数是:"+i);//打印完数
  14.                 }
复制代码
回复 使用道具 举报 1 0
zhxu188 发表于 2014-7-17 20:34
通过下面的小程序打印出的完数是4  28  496    所以24并不是完数

同感。。。。我也是这样写的。。。可是参考答案多了24,所以我就不明白了。。。。
回复 使用道具 举报
╄Tracyぺ 发表于 2014-7-17 23:08
同感。。。。我也是这样写的。。。可是参考答案多了24,所以我就不明白了。。。。 ...

我的代码:
public static void wanshu(){
               
                for(int i = 1; i <= 1000; i++){
                        int sum = 0;
                        String str = "";
                        for(int j = 1; j <= i/2; j++){
                                if(i%j==0){
                                        str+=j+"+";
                                        sum+=j;
                                }
                        }
                        if(i==sum){
                                System.out.println(i+"="+str.substring(0,str.length()-1));
                        }
                       
                }
        }
回复 使用道具 举报
╄Tracyぺ 发表于 2014-7-17 23:12
我的代码:
public static void wanshu(){
               

它提供的答案:
private static void compNumber(int n){
                int count = 0;
                System.out.println(n+"以内的完数:");
                for(int i=1;i<n+1;i++){
                        int sum = 0;
                        for(int j=1;j<i/2+1;j++){
                                if((i%j)==0){
                                        sum += j;
                                        if(sum==i){
                                  System.out.print(i+" ");
                                  if((count++)%5==0)
                                    System.out.println();
                            }
                                }
                        }
                }
        }
回复 使用道具 举报
╄Tracyぺ 发表于 2014-7-17 23:12
我的代码:
public static void wanshu(){
               

它提供的答案:
private static void compNumber(int n){
                int count = 0;
                System.out.println(n+"以内的完数:");
                for(int i=1;i<n+1;i++){
                        int sum = 0;
                        for(int j=1;j<i/2+1;j++){
                                if((i%j)==0){
                                        sum += j;
                                        if(sum==i){
                                  System.out.print(i+" ");
                                  if((count++)%5==0)
                                    System.out.println();
                            }
                                }
                        }
                }
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马