你的程序在哪里啊?
自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
#include <iostream>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <iomanip>
#include <cfloat>
#include <windows.h>
using namespace std;
class Factorial
{
private:
int n;
int length;
int base;
int * result;
void setLength()
{
int max = INT_MAX / n;
for( ; max>=base; base = base*10)
;
base = base / 10;
double test = 0;
for(int i = 2; i<= n; i++)
test += log10(i);
length = (int) (test) / (log10(base)) + 1;
}
void mul()
{
int carry = 0;
int r = 0;
for(int j = 3; j<=n; j++)
{
for(int i = length - 1; i>=0; i--)
{
r = result[i] * j + carry;
result[i] = r%base;
carry = r/base;
}
}
}
public:
Factorial(int n)
{
this->n = n;
base = 10;
setLength();
result = new int[length];
memset(result, 0, length*sizeof(int));
result[length-1] = 2;
}
~Factorial()
{
if(result)
delete [] result;
}
void fact()
{
memset(result, 0, length*sizeof(int));
result[length-1] = 2;
mul();
}
void display()
{
cout<<result[0];
cout.fill('0');
for(int i = 1; i<length; i++)
{
cout<<setw(log10(base))<<result[i];
}
cout<<endl;
}
};
int main()
{
LARGE_INTEGER listart,lifinish,lifrequency;
__int64 result;
int n = 10000;
QueryPerformanceCounter( &listart );
// excution
Factorial fac(n);
fac.fact();
QueryPerformanceCounter(&lifinish);
QueryPerformanceFrequency(&lifrequency);
result = (lifinish.QuadPart - listart.QuadPart)*1000000/lifrequency.QuadPart;
char dauer[20];
_i64toa(result, dauer, 10);
fac.display();
cout<<dauer<<endl;
system("pause");
return 0;
}
[QUOTE]#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#define CARRY 100000
clock_t start,end ;
int GetFactorialMemSize(int n);
long*InitiFactorialMem(int Size);
int Factorial(register long*FactorialMem,int n);
void PrintFactorial(long*FactorialMem,int Start);
int main(void)
{
int N,FactorialStart,FactorialMemSize ;
long*PFactorial ;
char Key='y' ;
while(Key=='y'||Key=='Y')
{
printf("N=?");
while(scanf("%d",&N)!=1)getchar();
FactorialMemSize=GetFactorialMemSize(N)/5+1 ;
PFactorial=InitiFactorialMem(FactorialMemSize);
start=clock();
FactorialStart=Factorial(PFactorial,N);
end=clock();
printf("\n计算%d!花费: %f 秒\n按回车打印....\n\n",N,(float)(end-start)/CLK_TCK);
getchar();
getchar();
start=clock();
PrintFactorial(PFactorial,FactorialStart);
end=clock();
printf("\n\n输出%d!花费: %f 秒\n",N,(float)(end-start)/CLK_TCK);
free(PFactorial);
printf("继续? y 或 Y 退出? 任意键\n");
Key=getchar();
}
return 0 ;
}
/*-----------------------------------------------------------------*/
int GetFactorialMemSize(int n)
{
double sum=1.0 ;
int i ;
for(i=1;i<=n;i++)
sum+=log10((long double)i);
return(int)sum ;
}
/*-------------------------------------------------------------------*/
long*InitiFactorialMem(int Size)
{
long*FactorialMem=(long*)calloc(Size,sizeof(long));
if(!FactorialMem)
{
printf(" Allocation Error !\n");
exit(1);
}
return FactorialMem ;
}
/*------------------------------------------------------------------*/
int Factorial(register long*FactorialMem,int n)
{
int NotZero=0,End=0,i,j ;
long int CARRY_number ;
register long int tem ;
FactorialMem[0]=1 ;
for(i=1;i<=n;i++)
{
CARRY_number=0 ;
for(j=NotZero;j<=End;j++)
{
tem=FactorialMem[j]*i+CARRY_number ;
CARRY_number=tem/CARRY ;
FactorialMem[j]=tem%CARRY ;
}
while(!FactorialMem[NotZero])NotZero++;
if(CARRY_number)FactorialMem[++End]=CARRY_number ;
}
return End ;
}
/*-------------------------------------------------------------------*/
void PrintFactorial(long*FactorialMem,int Start)
{
printf("%ld",FactorialMem[Start--]);
while(Start>=0)printf("%.5ld",FactorialMem[Start--]);
}[/QUOTE]
你传个执行文件上来,你的程序我编译不了,
KKK.cpp
Linking...
LIBC.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Release/KKK.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
KKK.exe - 2 error(s), 0 warning(s)
环境:PII 233 winme
我的
[QUOTE]N=?10000
计算10000!花费: 4.500000 秒
按回车打印....
............(略去)
0000000000000000000000000000000000
0000000000000000000000000000000000
0000000000000000000000000000000000
输出10000!花费: 6.590000 秒
继续? y 或 Y 退出? 任意键[/QUOTE]
你的,计算部分就要
23秒多