CSDN博客

习题 92：高精度算阶乘★★

/*题目描述：

10
20
100

3628800
2432902008176640000
933262154439441526816992388562667004907159682643816214685929
638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000

*/

#include <iostream>
#include <iomanip>
#define MAX_SIZE 10000
using namespace std;
//end is the last updated bit
int bigMulti(int *result, int n,int size, int end)
{
int i,j, carry, tmp;
carry = 0;
for(j = 1;j <= n; j++)
{
for(i = MAX_SIZE; --i >=end;)
{
tmp = result[i] * j + carry;
result[i] = tmp % 10000;
carry = tmp / 10000;
}
while(carry)
{
result[--end] = carry % 10000;
carry /= 10000;
}
}
return end;
}
int main(void)
{
int n, end;
int result[MAX_SIZE];//如果这里声明为char会导致速度减慢
while(EOF!=scanf("%d",&n))
{
memset(result,0,MAX_SIZE*sizeof(int));
result[MAX_SIZE - 1] = 1;// set result to 1
end = MAX_SIZE - 1;
end = bigMulti(result, n,MAX_SIZE, end);
cout << result[end++];
for(; end < MAX_SIZE;)
{
printf("%04d",result[end++]);
}
cout << endl;
//result[j] = 0;
//cout << result << endl;
}
return 0;
}
/*result:
78737Name: "younthu" Problem ID "92"Submit Time: 2008/9/28-13:35G++: Compile OKTest  1:    Accepted    Time = 0 msTest  2:    Accepted    Time = 0 msTest  3:    Accepted    Time = 267 ms--------------------------------Problem ID     92Test Result    AcceptedTotal Time     267 msTotal Memory   228 Kb / 2000 KbCode Length    817 Bytes*/

0 0