本题要求实现一个打印非负整数阶乘的函数.
函数接口定义:
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
现学现卖, 敲一遍高精度阶乘
- #include <stdio.h>
- void Print_Factorial ( const int N );
- int main()
- {
- int N;
- scanf("%d", &N);
- Print_Factorial(N);
- return 0;
- }
- void Print_Factorial ( const int N )
- {
- if(N<0){
- printf("Invalid input\n");
- return ;
- }
- int d[40000];
- d[0]=1;
- int t=0,tmp=0,carry=0;
- for(int i=1;i<=N;i++){
- for(int j=0;j<=t;j++){
- tmp=d[j]*i+carry;
- d[j]=tmp%10;
- carry=tmp/10;
- }
- while(carry!=0){
- d[++t]=carry%10;
- carry/=10;
- }
- }
- for(int i=t;i>=0;i--){
- printf("%d",d[i]);
- }
- printf("\n");
- }
来源: http://www.bubuko.com/infodetail-3100412.html