本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
void Print_Factorial ( const int N );
其中N是用户传入的参数,其值不超过1000。如果N是非负整数,则该函数必须在一行中打印出N!的值,否则打印“Invalid input”。
裁判测试程序样例:
#include <stdio.h>
void Print_Factorial ( const int N );
int main()
{
int N;
scanf("%d", &N);
Print_Factorial(N);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
15
输出样例:
1307674368000
void Print_Factorial ( const int N ){
if(N<0) printf("Invalid input");
else {
int num[10010];
num[0] = 1;
int len = 0;
int t = 0;
for(int i = 2; i <= N; i++)//用数组的每一位乘以下一个要乘的数
{
for(int j = 0; j <= len; j++)
{
t += num[j] * i;
num[j] = t%10;
t /= 10;
}
while(t)//遍历完当前长度还有剩余时,说明最后一次乘大于10 需要进位 长度需要加
{
len++;
num[len] = t%10;
t /= 10;
}
}
for(int i = len; i >= 0; i--)
printf("%d",num[i]);
}