基础编程题目集 6-10 阶乘计算升级版 (20分)

在这里插入图片描述

#include <stdio.h>
void Print_Factorial(const int N);
int main()
{
    int N;
    scanf("%d", &N);
    Print_Factorial(N);
    return 0;
}
void Print_Factorial(const int N)
{
    if (N < 0)
    {
        printf("Invalid input\n");
        return;
    }
    int d[40000];
    d[0] = 1;
    int t = 0, tmp = 0, carry = 0;
    for (int i = 1; i <= N; i++)
    {
        for (int j = 0; j <= t; j++)
        {
            tmp = d[j] * i + carry;
            d[j] = tmp % 10;
            carry = tmp / 10;
        }
        while (carry != 0)
        {
            d[++t] = carry % 10;
            carry /= 10;
        }
    }
    for (int i = t; i >= 0; i--)
    {
        printf("%d", d[i]);
    }
    printf("\n");
}
发布了318 篇原创文章 · 获赞 147 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44458489/article/details/105522884