Time Limit: 1Sec Memory Limit: 128MB Commits: 102 Resolved: 56
Topic description
Input a positive integer n, output the value of n!.
where n!=1*2*3*…*n.
The algorithm description
n! may be very large, and the range of integers that the computer can represent is limited, which requires the use of high-precision computing methods. Use an array A to represent a large integer a, A[0] represents the ones digit of a, A[1] represents the tens digit of a, and so on.
Multiplying a by an integer k becomes multiplying each element of array A by k, taking care to handle the corresponding carry.
First set a to 1, then multiply by 2, multiply by 3, and when it is multiplied to n, the value of n! is obtained.
where n!=1*2*3*…*n.
The algorithm description
n! may be very large, and the range of integers that the computer can represent is limited, which requires the use of high-precision computing methods. Use an array A to represent a large integer a, A[0] represents the ones digit of a, A[1] represents the tens digit of a, and so on.
Multiplying a by an integer k becomes multiplying each element of array A by k, taking care to handle the corresponding carry.
First set a to 1, then multiply by 2, multiply by 3, and when it is multiplied to n, the value of n! is obtained.
enter
The input contains a positive integer n, where n<=1000.
output
Output the exact value of n!.
sample input
10
Sample output
3628800
#include<iostream> using namespace std; const int maxn = 1000000; long long n,a[maxn],len=1; int main(void) { cin>>n; a[0]=1; for(int i=2;i<=n;i++) { long long g=0; for(int j=0;j<len;j++) a[j]*=i; for(int j=0;j<len;j++) { //cout<<"g="<<g<<endl; /* a[j]+=g; g=a[j]/10; a[j]%=10; */ if(a[j]>=10) { for(int k=0;k<len;k++) { if (a [len- 1 ]> = 10 ) len ++ ; a[k+1]+=a[k]/10; a[k]%=10; } } } //while(g) a[len++]+=g,g/=10; } for(int i=len-1;i>=0;i--) cout<<a[i]; return 0; }