第十三周——递归求阶乘

/**
*@University:烟台大学
*@Class计134~4
*@Author:薛富磊
*@Time:2013-11-22
*@Function:求1x3……(2n-1)xn
*@Args:n是任意一个奇数
*@Return:c
*/
#include <iostream>

using namespace std;

long  product(int);          //函数声明

int main()                   //主函数
{
    int n;
    cout << "Plese input:" << endl;
    cin>>n;
    cout<<"1x3……(2n-1)xn="<<product(n)<<endl;
    return 0;
}
long  product(int n)             //求阶乘的递归函数
{
    int s;                      //用作为存放乘积的变量
    if(n==1)
      s=1;                        //当n=1时 积为1
    else

      s=product(n-2)*n;            //当n>1 为前一个积乘以这个数

      return s;                  //将乘积带回主函数

}
//心得体会:一定要看好变量的使用
//居然用s写成了product;
//程序没问题 结果出错
//有兄弟
//不孤单

第十三周——递归求阶乘_第1张图片

你可能感兴趣的:(第十三周——递归求阶乘)