c语言-求完全数的一个算法

#include<iostream>

#include<math.h>

using namespace std;

int main()

{

    int n,i,j=0,k,a[100],sum=0;

    cout << "Please enter the upper bound\n";

    cin >> n;

    for(k=1;k<n+1;k++)

    {

        for(i=1;i<=k/2;i++)

            if((k%i)==0) 

            {

             a[j]=i;

              j++;

             }

        for(i=0;i<j;i++)

            sum+=a[i];

        if(sum==k)

        cout << sum <<' ';

        sum=0;j=0;

    }

    return 0;

}

猜你喜欢

转载自www.cnblogs.com/mathstudysharing/p/10476039.html