fjnu 1461 Factorials

本文介绍了一种计算大整数阶乘的方法,通过逐位相乘和进位处理实现,适用于计算0到100之间的整数阶乘。文章提供了一个具体的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

8
50

Sample Output

40320
30414093201713378043612608166064768844377641568960512000000000000

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;
}








 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值