本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
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 )
{
int n=N;
if(N==0)
{
printf("1\n");
return;
}
if(n<0)
{
printf("Invalid input\n");
return;
}
int ans[10000]={0};//答案保存时,位数低的在左边
int size=1;//乘到现在,有几位数
int bit=0;//进位的数
ans[1]=1;
for(int i=2;i<=n;i++)//阶乘到n
{
for(int j=1;j<=size;j++)//每个新数i乘以当前答案 ,乘的顺序 个 十 百
{
int tep=ans[j]*i+bit;
bit=tep/10;
ans[j]=tep%10;
}
while(bit)
{
ans[++size]=bit%10;
bit/=10;
}
}
for(int i=size;i>=1;i--)
{
printf("%d",ans[i]);
}
printf("\n");
}