PAT基础6-10

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

输出样例:

130767436800



void Print_Factorial ( const int N )
{
    int a[5000],up=0,temp=0,A=N;
    if(A<0)
    {
        printf("Invalid input");
        return;
    }
    for(int k=1;k<5000;k++)
    a[k]=0;
    a[0]=1;
    for(int i=1;i<=A;i++)
    {
        for(int j=0;j<5000;j++)
        {
            temp=(a[j]*i+up)/10;
            a[j]=(a[j]*i+up)%10;
            up=temp;
        }
    }

    int t=4999;
    while(a[t]==0&&t>0)
    {
        t--;
    }
    while(t+1)
    {
        printf("%d",a[t]);
        t--;
    }
}
原文地址:https://www.cnblogs.com/lxzbky/p/10470246.html