zoukankan      html  css  js  c++  java
  • PAT 基础编程题目集 6-10 阶乘计算升级版 (20 分)

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

    函数接口定义:

    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
    ");
    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("
    ");
    35 }
  • 相关阅读:
    基于JavaMail开发邮件发送器工具类
    微服务架构
    图像识别
    Manacher
    左偏树
    虚树
    动态树(Link-Cut Tree)
    Splay
    扩展中国剩余定理(EXCRT)
    K-D树
  • 原文地址:https://www.cnblogs.com/shixinzei/p/11069166.html
Copyright © 2011-2022 走看看