#include<iostream>
#include<stdlib.h>
using namespace std ;
int main()
{
int a[800] = {0} ;
int m = 0 ;
int n , b , t , w=0 ;
a[0]=a[1]=1 ;
for(int j=2 ; j<=1000 ; j++)
{
n = a[0] ;
for(int i=1 ; i<=n ; i++)
{
a[i] = a[i] * j + m ;
if(a[i] > 9999)
{
t = a[i] / 10000 ;
a[i] = a[i] % 10000 ;
m = t ;
if(i == n)
{
while(m > 9999)
{
t = a[i] / 10000 ;
a[i] = a[i] % 10000 ;
m = t ;
a[0]++ ;
}
if(m != 0)
{
i++ ;
a[i] = m ;
a[0]++ ;
m = 0 ;
}
}
}
else
m = 0 ;
}
}
for(int i=799 ; i>=1 ; i--)
if(a[i] > 0)
{
b = i ;
break ;
}
while(a[b])
{
w = w + 1 ;
a[b] = a[b] / 10 ;
}
cout << "1000的阶乘一共有" << (a[0]-1) * 4 + w << "位!" ;
cout << endl ;
for(int i=b-1 ; i>=1 ; i--)
{
if(a[i] > 999)
cout << a[i] ;
if(a[i] >= 100 && a[i] <= 999)
cout << "0" << a[i] ;
if(a[i] >= 10 && a[i] <= 99)
cout << "00" << a[i] ;
if(a[i] > 0 && a[i] <= 9)
cout << "000" << a[i] ;
if(a[i] == 0)
cout << "0000" ;
}
cout << endl ;
system("pause") ;
}
//1000! = 1 * 2 * 3 * 4 * ... * 1000
//两边取10的对数
//log(1000!) = log(1*2*3*4* ... * 1000)
// = log(1) + log(2) + log(3) + ... + log(1000)
//编个程序把右边算出来就可以了,注意用double来保存结果。
//log(1000!) = 2567.60
//把结果取整再加一,就得到1000!的位数为2568
//#include<stdio.h>
//#include<math.h>
//int main()
//{ double sum = 0.0;
// int i;
// for(i=1;i<=1000;++i)
// sum += log10(i);
// printf("%d\n",(int)sum + 1);
// return 0;
//}