6-10 阶乘计算升级版(浙大PTA-C语言编程基础题目集)

做题时只提交核心函数的代码即可

// 核心函数

#define MAX_CAPACITY 5000 // 宏定义,MAX_CAPACITY的值为5000

struct LargeNumber
{
    char *number;
    int length;
    int capacity;
};
typedef struct LargeNumber LargeNumber;

void Print_Factorial(const int N);
void Factorial(LargeNumber *result, const int num);
void PartialMultiplication(LargeNumber *pDest, int multiplier);
void OutputProduct(LargeNumber *product);

void Print_Factorial(const int N)
{
    if (N < 0)
    {
        printf("Invalid input\n");
        return;
    }

    LargeNumber product;
    product.capacity = MAX_CAPACITY;
    product.length = 0;
    product.number = (char *)malloc(product.capacity * sizeof(char));

    Factorial(&product, N);

    OutputProduct(&product);

    free(product.number);
}

void Factorial(LargeNumber *result, const int num)
{
    result->number[0] = 1;
    result->length = 1;

    for (int cnt = 2; cnt <= num; ++cnt)
    {
        PartialMultiplication(result, cnt);
    }
}

void PartialMultiplication(LargeNumber *pDest, int multiplier)
{
    int carrier = 0;
    for (int idx = 0; idx < pDest->length; ++idx)
    {
        int tmp = pDest->number[idx] * multiplier + carrier;
        pDest->number[idx] = tmp % 10;
        carrier = tmp / 10;
    }

    while (carrier)
    {
        if (pDest->length > pDest->capacity)
        {
            printf("Out of capacity\n");
            return;
        }

        pDest->number[pDest->length] = carrier % 10;
        carrier /= 10;
        ++(pDest->length);
    }
}

void OutputProduct(LargeNumber *product)
{
    for (int idx = product->length - 1; idx >= 0; --idx)
    {
        putchar(product->number[idx] + '0');
    }
    putchar('\n');
}

本题要求实现一个打印非负整数阶乘的函数。

函数接口定义:

void Print_Factorial ( const int N );

其中N是用户传入的参数,其值不超过1000。如果N是非负整数,则该函数必须在一行中打印出N!的值,否则打印“Invalid input”

完整的可执行代码(可以直接在本地编译并运行):

#include <stdio.h> // printf, scanf的函数声明在此头文件中

void Print_Factorial(const int N); // 函数声明

int main(void) // main函数,程序入口
{
    int N = 0;

    scanf("%d", &N); // 从标准输入中读取一个整数,赋值给N
    Print_Factorial(N);

    return 0;
}

#define MAX_CAPACITY 5000 // 宏定义,MAX_CAPACITY的值为5000

struct LargeNumber
{
    char *number;
    int length;
    int capacity;
};
typedef struct LargeNumber LargeNumber;

void Print_Factorial(const int N);
void Factorial(LargeNumber *result, const int num);
void PartialMultiplication(LargeNumber *pDest, int multiplier);
void OutputProduct(LargeNumber *product);

void Print_Factorial(const int N)
{
    if (N < 0)
    {
        printf("Invalid input\n");
        return;
    }

    LargeNumber product;
    product.capacity = MAX_CAPACITY;
    product.length = 0;
    product.number = (char *)malloc(product.capacity * sizeof(char));

    Factorial(&product, N);

    OutputProduct(&product);

    free(product.number);
}

void Factorial(LargeNumber *result, const int num)
{
    result->number[0] = 1;
    result->length = 1;

    for (int cnt = 2; cnt <= num; ++cnt)
    {
        PartialMultiplication(result, cnt);
    }
}

void PartialMultiplication(LargeNumber *pDest, int multiplier)
{
    int carrier = 0;
    for (int idx = 0; idx < pDest->length; ++idx)
    {
        int tmp = pDest->number[idx] * multiplier + carrier;
        pDest->number[idx] = tmp % 10;
        carrier = tmp / 10;
    }

    while (carrier)
    {
        if (pDest->length > pDest->capacity)
        {
            printf("Out of capacity\n");
            return;
        }

        pDest->number[pDest->length] = carrier % 10;
        carrier /= 10;
        ++(pDest->length);
    }
}

void OutputProduct(LargeNumber *product)
{
    for (int idx = product->length - 1; idx >= 0; --idx)
    {
        putchar(product->number[idx] + '0');
    }
    putchar('\n');
}

输入样例:

15

输出样例:

1307674368000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James_Xue_2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值