转载自:C语言求大数阶乘
笔者用C++改写(练习)了下:
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int i,j,n,temp,d=0,carry; 5 int a[3000]; 6 a[0]=1; 7 cin>>n; 8 for (i=2;i<=n;i++){ 9 for(j=0,carry=0;j<=d;j++){ 10 temp=a[j]*i+carry; 11 a[j]=temp%10; 12 carry=temp/10; 13 } 14 while(carry){ 15 d++; 16 a[d]=carry%10; 17 carry=carry/10; 18 } 19 } 20 cout<<n<<"!="; 21 for(j=d;j>=0;j--) 22 cout<<a[j]; 23 cout<<endl; 24 return 0; 25 }