首页 > 其他 > 详细

PAT 基础编程题目集 6-10 阶乘计算升级版 (20 分)

时间:2019-06-22 16:14:05      阅读:137      评论:0      收藏:0      [点我收藏+]

本题要求实现一个打印非负整数阶乘的函数。

函数接口定义:

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

现学现卖,敲一遍高精度阶乘
 1 #include <stdio.h>
 2 void Print_Factorial ( const int N );
 3 int main()
 4 {
 5     int N;
 6     
 7     scanf("%d", &N);
 8     Print_Factorial(N);
 9     return 0;
10 }
11 void Print_Factorial ( const int N )
12 {
13     if(N<0){
14         printf("Invalid input\n");
15         return ;
16     }
17     int d[40000];
18     d[0]=1;
19     int t=0,tmp=0,carry=0;
20     for(int i=1;i<=N;i++){
21         for(int j=0;j<=t;j++){
22             tmp=d[j]*i+carry;
23             d[j]=tmp%10;
24             carry=tmp/10;
25         }
26         while(carry!=0){
27             d[++t]=carry%10;
28             carry/=10;
29         }
30     }
31     for(int i=t;i>=0;i--){
32         printf("%d",d[i]);
33     }
34     printf("\n");
35 }

 

PAT 基础编程题目集 6-10 阶乘计算升级版 (20 分)

原文:https://www.cnblogs.com/shixinzei/p/11069166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!