注册 登录
编程论坛 C++教室

这问题出在什么地方 2000以内的完数

fz19910125 发布于 2011-11-18 14:42, 678 次点击
程序代码:
// 2000以内所有的完数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;
void IfWan(int a,int* end)
{
    int j,sum=0,count=0;
    for(j=1;j<a;j++)
    {
        if(a%j==0)
        {
            sum+=j;
        }
    }
    if(sum==a)
    {
        end[count]=a;
        count++;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int jg[100]={0};
    for(int i=1;i<2000;i++)
    {
        IfWan(i,jg);
    }   
    for(int j=0;j<100;j++)
    {
        cout<<jg[j]<<endl;
    }
    system("pause");
    return 0;
}
怎么输出的结果为496啊??
2 回复
#2
waterstar2011-11-23 20:50
// 2000以内所有的完数.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include "iostream"
using namespace std;
bool IfWan(int a)
{
    int j,sum=0,count=0;
    for(j=1;j<a;j++)
    {
        if(a%j==0)
        {
            sum+=j;
        }
    }
    if(sum==a)
        return true;
   
    return false;
}

int main()
{
    int jg[100]={0};
    int    j = 0;
    for(int i=1;i<2000;i++)
    {
        if (IfWan(i))
            jg[j++] = i;
    }   
    for(j=0;j<100;j++)
    {
        cout<<jg[j]<<endl;
    }
    system("pause");
    return 0;
}

哪有那么多完数啊,到现在为止才找出47个而已,第47个数有好几百万位,用那么多空间太浪费。
#3
hxcet2011-11-24 12:09
因为你用for循环每代入一次IfWan函数,函数内的count又被重置为0,函数体内最后的count++;实际上一直没有起到作用。。。所以最后你只把end[0]=496存入了。。
1