fjnu 1461 Factorials

来源:互联网 发布:酒心巧克力 知乎 编辑:程序博客网 时间:2024/04/29 18:25

Description

The factorial of n, (written as n!) is n * (n - 1) * (n - 2) * ... * 2 * 1. 0! = 1 (go lookup the Gamma function to find out why). In this problem, you'll be calculating the factorial of an integer between 0 and 100. 100! has no more than 200 digits.

Input

There are 1 or more lines of input. Each line has a single integer, N, on it, such that 0 <= N <= 100.

Output

For each line of input, your program should print out N!.

Sample Input

850

Sample Output

4032030414093201713378043612608166064768844377641568960512000000000000

KEY:这题是求大整数的阶乘,我是采用先乘后进位的办法,有点慢就是了;

 

Source:

#include
<iostream>
using namespace std;

void Factorial(int n)
{
    
int a[210];
    
int i,j,k;
    
for(i=0;i<=200;i++)
    
{
        a[i]
=0;
    }

    a[
1]=1;
    k
=1;
    
for(i=2;i<=n;i++)
    
{
        
for(j=1;j<=k;j++)
        
{
            a[j]
=a[j]*i;
        }

        
for(j=1;j<=k;j++)
        
{
            a[j
+1]+=a[j]/10;
            a[j]
=a[j]%10;
        }

        
while(a[j]>9)
        
{
            a[j
+1]+=a[j]/10;
            a[j]
=a[j]%10;            
            
if(a[j+1]>0) j++;
        }

        
if(a[j]>0) k=j;
    }

    
for(i=k;i>0;i--)
        cout
<<a[i];
    cout
<<endl;
}


int main()
{
//    freopen("fjnu_1185.in","r",stdin);
    int n;
    
while(cin>>n)
    
{
        Factorial(n);
    }

    
return 0;
}