zoukankan      html  css  js  c++  java
  • 2018/12/22

    简单阶乘计算 

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

    函数接口定义:

    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, NF);
        else printf("Invalid input
    ");
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    5
    

    输出样例:

    5! = 120

    代码:
    int Factorial( const int N )
    {
        int i, sum = 1;
        
        if(N<0)
        return 0; 
        
        for(i=1;i<=N;i++)
        {
            sum *= i;
        }
        return sum;
    }

    回顾了下之前得内容,巩固下学习,这题发现const的作用。const修饰的数据类型是指常类型,常类型的变量或对象的值是不能被重新赋值的。

     
  • 相关阅读:
    C++赌博游戏
    数据挖掘--数据准备
    非线性维归约Isomap
    ClampedCubicSpline
    Romberg算法
    列表与数组
    HttpClient调用webApi时注意的小问题
    Week4
    Week3
    Week2
  • 原文地址:https://www.cnblogs.com/zw431387/p/10162137.html
Copyright © 2011-2022 走看看