关于完美数的程序问题 时间不能小于3s

创客征途 2014-01-05 10:51:27
本期csdn的编程挑战,完美数问题
要求1<=x<=y<=2*10^9;
并且程序运行时间小于3秒
我输入 x=10^7,y=10^8,就6.8秒,我的程序,
#include<iostream.h>

#include <time.h>//x=1000000 y=10000000 t=0.716
void main() //x=
{
int a[10],ee;
int num,num2,num3;
int cnt=0;

int x,y;
cin>>x>>y;
clock_t t1,t2;
t1=clock();
for(num=x;num<=y;num++)
{
ee=0;
num2=num /10000;
num3=num2/10000;
a[0]=num%10;
a[1]=num/10%10;
a[2]=num/100%10;
a[3]=num/1000%10;
a[4]=num2%10;
a[5]=num2/10%10;
a[6]=num2/100%10;
a[7]=num2/1000%10;
a[8]=num3%10;
a[9]=num3/10%10;
for(int i=9;i>=8;i--)
{
if( a[i]!=0)
{
if(num3%a[i]==0)
ee++;

else
{ee=0;goto eeend;}

}
else ee++;

}
for(i=7;i>=4;i--)
{
if( a[i]!=0)
{
if(num2%a[i]==0)
ee++;

else
{ee=0;goto eeend;}

}
else ee++;

}
for( i=3;i>=0;i--)
{
if( a[i]!=0)
{
if(num%a[i]==0)
ee++;

else
{ee=0;goto eeend;}

}
else ee++;

}

eeend: if(ee==10)
cnt++;


}




cout<<"isperfect number="<<cnt<<endl;
t2=clock();
cout<<"run time is:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
}
...全文
376 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
创客征途 2014-02-05
  • 打赏
  • 举报
回复
好好,我试试
匹夫 2014-01-23
  • 打赏
  • 举报
回复
#include<iostream>
#include <algorithm>

#include <time.h>//x=1000000 y=10000000 t=0.716

int a[20], b[160000], c[8000000];
int alen, blen, clen;


int d[1000000];
int dlen;



int gcd(int m, int n)
{
    int r;
    for(;;)
    {
        if (m%n == 0)
            return n;
        r = m%n;
        m = n;
        n = r;
    }
}
int lgcd(int m, int n)
{
    if (m == 0 && n == 0) return 1;
    if (m == 0) return n;
    if (n == 0) return m;
    return m/gcd(m,n)*n;
}

int my_search(int k)
{
    int f = 0, t = clen-1, j;    
    j = (f+t+1) / 2;
    while ( f < t)
    {
        if (c[j] <= k)
            f = j;
        else
            t = j-1;
        j = (f+t+1) / 2;
    }
    return f;
}

int cmp(const void * p, const void * q)
{
    return *((int*)p) - *((int*)q);
}

void what();

using namespace std;
int main()      //x=
{	
    clock_t t1,t2;
    t1=clock();

    int i, j, k, t;
    for (i = 0; i < 20; i++)
    {
        t = lgcd(i/10, i%10);
        if ( i % t == 0 )
            a[alen++] = i;        
    }
    printf("%d\n", alen);

    for (i = 0; i < 10000; i++)
    {
        t = lgcd( lgcd( lgcd( i%10, i/10%10), i/100%10 ), i/1000);
        for (j = 0; j < alen; j++)
            if ( (10000*a[j]+i) % t == 0 )
                b[blen++] = 10000*a[j]+i;
    }

    printf("%d\n", blen);


    for (i = 0; i < 10000; i++)
    {
        if (i == 130)
        {
            int kkkk = 0;
        }
        t = lgcd( lgcd( lgcd( i%10, i/10%10), i/100%10 ), i/1000);
        for (j = 0; j < blen; j++)
            if ( (10000*b[j]+i) % t == 0 )
                c[clen++] = 10000*b[j]+i;
    }

    printf("%d\n", clen);
    std::sort(c, c+clen);

    //qsort( c, clen, sizeof(int), cmp);
    

    int x = 10000000;
    int y = 100000000;

    int yy = my_search(y);
    int xx = my_search(x-1);

    cout << yy-xx << endl;

     //cout << xx << endl;
     // cout << yy << endl;

    t2=clock();

    cout<<"run time is:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;


    what();

    for (i = 0; i < dlen; i++)
        if (c[i] != d[i])
            break;

    return 0;



}


void what()
{
    int aaaaa[10],ee;
    int  num,num2,num3;
    int cnt=0;

    int   x,y, i;

    x = 10000000;
    y = 100000000;
        
    clock_t t1,t2;
    t1=clock();
    for(num=x;num<=y;num++)
    {	
    ee=0;
    num2=num /10000;
        num3=num2/10000;
    aaaaa[0]=num%10;
    aaaaa[1]=num/10%10;
    aaaaa[2]=num/100%10;
    aaaaa[3]=num/1000%10;
    aaaaa[4]=num2%10;
    aaaaa[5]=num2/10%10;
    aaaaa[6]=num2/100%10;
    aaaaa[7]=num2/1000%10;
    aaaaa[8]=num3%10;
    aaaaa[9]=num3/10%10;
    for( i=9;i>=8;i--)
    {	
        if( aaaaa[i]!=0)	
        {
            if(num3%aaaaa[i]==0)
                ee++;
            else 
            {
                ee=0;goto eeend;
            }
        }
        else ee++;

    }

    for(i=7;i>=4;i--)
    {	
        if( aaaaa[i]!=0)	
        {
            if(num2%aaaaa[i]==0)
                ee++;
            else 
            {ee=0;goto eeend;}
        }
        else ee++;
    }

    for( i=3;i>=0;i--)
    {	
        if( aaaaa[i]!=0)	
        {
            if(num%aaaaa[i]==0)
                ee++;
            else 
            {ee=0;goto eeend;}
        }
        else ee++;
    }

    eeend:  	 
    if(ee==10)
    {
        cnt++;
        d[dlen++] = num;
    }


    }




    cout<<"isperfect number="<<cnt<<endl;
    t2=clock();
    cout<<"run time is:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;

}
把范围内所有的值都算出来,还在不算大,少于800万,然后排好序,后面的计算就不怎么费时间了,(排序可以换成基数排序,可能节省一些时间),大概3秒内能跑完 (最大的那个数忘记往里放了)
创客征途 2014-01-23
  • 打赏
  • 举报
回复
没有人看懂吗,我想知道答案
sduxiaoxiang 2014-01-18
  • 打赏
  • 举报
回复
神奇的代码。。
lm_whales 2014-01-18
  • 打赏
  • 举报
回复
你的电脑还不错
创客征途 2014-01-06
  • 打赏
  • 举报
回复
本期csdn 编程挑战第二题,啊应该都有人知道了
u012997273 2014-01-06
  • 打赏
  • 举报
回复
没看懂啊,楼主把题目说清楚些,一大堆代码没人愿意看啊
创客征途 2014-01-05
  • 打赏
  • 举报
回复
论坛人都走光了吗,徒有空城
创客征途 2014-01-05
  • 打赏
  • 举报
回复
没人来啊,ee是记录整除个数的,等于10则为 完美数

33,319

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧