C++用递归函数,如何自定义输入一个函数值,求阶乘的值?以下为不是自定义的,如何修改
利用递归“计算n的阶乘”,!n!5=5*4*3*2*1factorial(5)=5*factorial(4);factorial(4)=4*factorial(3);fa...
利用递归“计算n的阶乘”, !n !5 = 5*4*3*2*1
factorial(5) = 5 * factorial(4);
factorial(4) = 4 * factorial(3);
factorial(3) = 3 * factorial(2);
factorial(2) = 2 * factorial(1);
factorial(1) = 1;
#include "stdafx.h"
#include <iostream.h>
int factorial(int n);
int main(int argc, char* argv[])
{
cout << "5的阶乘是" << factorial(5) << "\n";
return 0;
}
int factorial(int n)
{
if((n==0) || (n==1) )
{
return 1;
}
else
{
return n*factorial(n-1);
}
} 展开
factorial(5) = 5 * factorial(4);
factorial(4) = 4 * factorial(3);
factorial(3) = 3 * factorial(2);
factorial(2) = 2 * factorial(1);
factorial(1) = 1;
#include "stdafx.h"
#include <iostream.h>
int factorial(int n);
int main(int argc, char* argv[])
{
cout << "5的阶乘是" << factorial(5) << "\n";
return 0;
}
int factorial(int n)
{
if((n==0) || (n==1) )
{
return 1;
}
else
{
return n*factorial(n-1);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询