据Knocker 说,他们最快的速度为 7.9 秒左右。
你们的任务就是超越这个速度,程序对语言不做限制。
考虑到对速度测试的公正性,特邀请 Knocker 测试大家的程序。
比赛截至时间为中国时间 11月1日 下午16:00
到比赛截至,我将公布我的代码。
[此贴子已经被作者于2005-10-31 7:01:08编辑过]
[此贴子已经被作者于2005-10-31 7:01:08编辑过]
#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;
}