注册 登录
编程论坛 VC++/MFC

哪位高人帮忙看一下,求完全数程序用vc++2010编译后执行速度比devcpp还慢是什么原因?

lzb6689 发布于 2013-10-22 09:45, 598 次点击
vc++2010中源代码
// 完全数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int wqs(long long n,long long *a)
{
    long long i,j,k,l;
    double x,z;
    int r;
    z=n;
    x=sqrt(z);l=(long long)x+1;
    //cout<<"1";
    a[1]=1;r=1;
    i=1;
    while (i<l)
    {
        i++;
        while ((n%i)==0)
        {
            n=n/i;
            //cout<<"x"<<i;
            r++;
            a[r]=i;
        }
        x=sqrt(1.0*n);l=(long long)x+1;
    }
    if(n>1)
    {
        r++;
        a[r]=n;
    }   
     //cout<<"x"<<n;
     return r;
}

int ysfj(long long n,long long *a)
{
    long long i,j,k,l;   
    int r;        
    a[1]=1;r=1;
    i=1;
    l=n/2;
    while (i<l)
    {
        i++;
        if((n%i)==0)
        {
            r++;
            a[r]=i;
        }
        
    }   
     return r;
}


int main(int argc, char *argv[])
{
    clock_t begin,end;
    double t1;
    long long *a;
    long long m,n,h,w;
    int i,k,r;
    cout<<"n=";
    cin>>n;
    cout<<endl;
    begin=clock();
    a=new long long[10000];
    r=wqs(n,a);
    cout<<n<<" = ";
    for(i=1;i<=r;i++)
    {
      if(i==1)    cout<<a[i];else cout<<" x "<<a[i];
    }
    cout<<endl;
    cout<<endl;
    h=0;
    while(h<n)
    {
        h++;
        r=ysfj(h,a);
        w=0;
        for(k=1;k<=r;k++)
        {
            w=w+a[k];
        }
        if (w==h)
        {
            cout<<h<<" = ";
            for(i=1;i<=r;i++)
            {
                if(i==1)    cout<<a[i];else cout<<" + "<<a[i];
            }
            cout<<endl;
        }
    }
    end=clock();
    t1=(double)(end-begin)/CLOCKS_PER_SEC;
    cout<<"t1="<<t1<<endl;
    delete[] a;
    system("pause");
    return 0;
}

8 回复
#2
lzb66892013-10-22 09:51
只有本站会员才能查看附件,请 登录

vc++2010编译后执行结果
#3
lzb66892013-10-22 09:51
dev-c++源代码:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int wqs(long long n,long long *a)
{
    long long i,j,k,l;
    double x,z;
    int r;
    z=n;
    x=sqrt(z);l=(long long)x+1;
    //cout<<"1";
    a[1]=1;r=1;
    i=1;
    while (i<l)
    {
        i++;
        while ((n%i)==0)
        {
            n=n/i;
            //cout<<"x"<<i;
            r++;
            a[r]=i;
        }
        x=sqrt(1.0*n);l=(long long)x+1;
    }
    if(n>1)
    {
        r++;
        a[r]=n;
    }   
     //cout<<"x"<<n;
     return r;
}

int ysfj(long long n,long long *a)
{
    long long i,j,k,l;   
    int r;        
    a[1]=1;r=1;
    i=1;
    l=n/2;
    while (i<l)
    {
        i++;
        if((n%i)==0)
        {
            r++;
            a[r]=i;
        }
        
    }   
     return r;
}


int main(int argc, char *argv[])
{
    clock_t begin,end;
    double t1;
    long long *a;
    long long m,n,h,w;
    int i,k,r;
    cout<<"n=";
    cin>>n;
    cout<<endl;
    begin=clock();
    a=new long long[10000];
    r=wqs(n,a);
    cout<<n<<" = ";
    for(i=1;i<=r;i++)
    {
      if(i==1)    cout<<a[i];else cout<<" x "<<a[i];
    }
    cout<<endl;
    cout<<endl;
    h=0;
    while(h<n)
    {
        h++;
        r=ysfj(h,a);
        w=0;
        for(k=1;k<=r;k++)
        {
            w=w+a[k];
        }
        if (w==h)
        {
            cout<<h<<" = ";
            for(i=1;i<=r;i++)
            {
                if(i==1)    cout<<a[i];else cout<<" + "<<a[i];
            }
            cout<<endl;
        }
    }
    end=clock();
    t1=(double)(end-begin)/CLOCKS_PER_SEC;
    cout<<"t1="<<t1<<endl;
    delete[] a;
    system("pause");
    return 0;
}
#4
wp2319572013-10-22 09:56
vs2010下:

实际运行效果:

D:\c_source\t7\Debug>t7
n=369

369 = 1 x 3 x 3 x 41

1 = 1
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
t1=0
请按任意键继续. . .

D:\c_source\t7\Debug>t7
n=25

25 = 1 x 5 x 5

1 = 1
6 = 1 + 2 + 3
t1=0
请按任意键继续. . .

#5
lzb66892013-10-22 09:56
devcpp编译后执行结果
只有本站会员才能查看附件,请 登录

#6
wp2319572013-10-22 09:59
vs2010下运行效果

D:\c_source\t7\Debug>t7
n=123456

123456 = 1 x 2 x 2 x 2 x 2 x 2 x 2 x 3 x 643

1 = 1
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
t1=47.234
请按任意键继续. . .



#7
wp2319572013-10-22 09:59
不知道你的t1是虾米东东
#8
lzb66892013-10-22 10:59
呵呵! t1只不过是随便设置的一个double变量而已。
#9
lzb66892013-10-22 11:01
呵呵!没说清楚, t1是计算运算所用时间的变量。
1