0
点赞
收藏
分享

微信扫一扫

HDU 1042 N! (大整数阶乘)

ITWYY 2023-04-14 阅读 32


这道题开始并不会,是看了别人的代码,自己又改造了一下,代码如下:(PS:这个时候自带大整数运算的java就有优势了)

#include <bits/stdc++.h>

using namespace std;

const int N = 20000 + 10;

int ans[N];
void fact(int n)
{
    ans[0] = ans[1] = 1;
    int tot = 1;
    for(int i = 1; i <= n; i++)
    {
        int val = 0;
        for(int j = 1; j <= tot; j++)
        {
            ans[j] = ans[j] * i + val;
            val = ans[j] / 10000, ans[j] %= 10000;
        }
        if(val) ans[++tot] = val;
    }
    for(int i = tot; i >= 1; i--)
    {
        if(i == tot) printf("%d", ans[i]);
        else printf("%04d", ans[i]);
    }
    printf("\n");
}
int main()
{
    int n;
    while(~ scanf("%d", &n))
    {
        fact(n);
    }
    return 0;
}





举报

相关推荐

0 条评论