做题时只提交核心函数的代码即可
// 核心函数
#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