《算法笔记》系列: 问题 B: N的阶乘

 题目来源:http://codeup.cn/problem.php?cid=100000593&pid=1

题目描述

输入一个正整数N,输出N的阶乘。

输入

正整数N(0<=N<=1000)

输出

输入可能包括多组数据,对于每一组输入数据,输出N的阶乘

样例输入

0
4
7

样例输出

1
24
5040
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define nMAX 3001

int lenth; //lenth: the length of list

struct bign{
    int d[nMAX],c;
}list[1001]={0};

bign multi(bign b, int n){
    for(int j=0;j<b.c;j++){
        b.d[j]=b.d[j]*n;
    }
    for(int j=0;j<b.c;j++){
        int t=b.d[j];
        b.d[j]=0;
        for(int i=0;i<5;i++){
            if(t>0){
                b.d[j+i]+=t%10;
                t/=10;
                if(j+i>=b.c)b.c=j+i+1;
            }
            else break;
        }
    }
    return b;
}

bign get_factorial(int n){
    if(n<lenth)return list[n];
    for(int i=lenth;i<=n;i++){
        list[i]=multi(list[i-1],i);
    }
    lenth=n+1;
    return list[n];
}

int main()
{
    int N;
    lenth=1;
    list[0].c=1;
    list[0].d[0]=1;
    while(EOF != scanf("%d",&N)){
        bign ans;
        ans = get_factorial(N);
        int t=ans.c;
        while(t--)
            printf("%d",ans.d[t]);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值