本题要求实现一个打印非负整数阶乘的函数。
函数接口定义:
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 ans[3000];
int max = 1;
for(int i = 0;i < 3000;i++) ans[i] = -1;
ans[0] = 1;
int x = 0;
if(N < 0)
printf("Invalid input");
else{
for(int i = 1;i <= N;i++){
int j;
for(j = 0;ans[j] != -1;j++){
ans[j] = ans[j]*i + x;
x = ans[j] / 10;
ans[j] %= 10;
}
while(x){
ans[j++] = x % 10;
x /= 10;
}
max = j;
}
for(int i = max-1;i >= 0;i--){
if(ans[i] != -1)
printf("%d",ans[i]);
}
}
}