大数阶乘

输入一个正整数n,输出n!的值。

其中n!=123…n。

算法描述

n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法。使用一个数组A来表示一个大整数a,A[0]表示a的个位,A[1]表示a的十位,依次类推。

将a乘以一个整数k变为将数组A的每一个元素都乘以k,请注意处理相应的进位。

首先将a设为1,然后乘2,乘3,当乘到n时,即得到了n!的值。

输入

输入包含一个正整数n,n<=1000。

输出

输出n!的准确值。

样例输入

10

样例输出

3628800

代码:

#include <iostream>
#include <cstdio>
#include<map>
#include<algorithm>
#include<math.h>
#include<string.h>
#include <cmath>
#include<sstream>
using namespace std;
#define N   30000
int a[N]= {1};

int main()
{
    int n,pos,maxpos = 0;
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        int temp = 0;
        for (pos = 0; pos <= maxpos ; pos++)
        {
            temp += a[pos]*i;
            a[pos] = temp%10;
            temp /=10;
        }
        while(temp)
        {
            a[pos++] = temp%10;
            temp /=10;
        }
        pos--;
        maxpos = pos > maxpos? pos :maxpos;
    }
    maxpos++;
    while(maxpos--)
        cout<<a[maxpos];
    return 0;
}

转载于:https://www.cnblogs.com/cmmdc/p/6767257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值