PAT 6-8

6-8 简单阶乘计算 (10 分)

本题要求实现一个计算非负整数阶乘的简单函数。

函数接口定义:

int Factorial( const int N );

其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。

裁判测试程序样例:

#include <stdio.h>

int Factorial( const int N );

int main()
{
    int N, NF;

    scanf("%d", &N);
    NF = Factorial(N);
    if (NF)  printf("%d! = %d\n", N, NF);
    else printf("Invalid input\n");

    return 0;
}

/* 你的代码将被嵌在这里 */
int Factorial( const int N )
{
    int n=N;    //N是const修饰的不能修改
    int ret=1;
    if(n>=0)
    {
        for(int i=2;i<=n;i++)
        {
            ret*=i;
        }
        return ret;
    }
    return 0;
}

 

posted @ 2021-10-20 16:49  疯在其中不愿醒  阅读(45)  评论(0)    收藏  举报