N!

本文介绍了一种使用C++实现的大数阶乘算法,适用于计算10000以内的整数阶乘。通过数组存储大数并逐位相乘的方式,解决了整型溢出的问题。

给出一个数字N,求出其N!,,N在10000以内。

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>

using namespace std;


int go[19000];
int n ;

int main()
{
    int i,j,k;
    while (cin>>n)
    {
        if (n==0||n==1)
            cout<<'1'<<endl;
        else
        {
            memset(go,0,sizeof(go));
            int l=1;
            go[0]=1;
            for (i=2;i<=n;i++)
            {
                for (j=0;j<l;j++)
                {
                    go[j]=go[j]*i;
                
                }
                for (j=0;j<l;j++)
                {
                    if (go[j]>=10000)
                    {
                        go[j+1]=go[j]/10000+go[j+1];
                        go[j]=go[j]%10000;
                        if (j==l-1)
                            l++;
                    }
                }
            }
            cout<<go[l-1];
            for (i=l-2;i>=0;i--)
            {
                if (go[i]<10)
                    cout<<"000";
                else if (go[i]<100)
                    cout<<"00";
                else if (go[i]<1000)
                    cout<<"0";
                cout<<go[i];
            }
            cout<<endl;

        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值