要诀:
递归部分的边界条件就是递推公式中的边界条件(即已知条件)
递推部分的主体部分就是递推公式中的主体部分
eg:求n!
已知f(n)=f(n-1)*n, f(1)=1;
程序中就可以这么写:

               int factorial_Recursion(int ans)
{
	if (ans == 1)
	{
		return 1;
	}
	else
	{
		return factorial_Recursion(ans - 1)*ans;
	}
}

接下来只需在主程序中调用:

#include "stdafx.h"
#include<iostream>
using namespace std;
int factorial_Recursion(int ans)
{
	if (ans == 1)
	{
		return 1;
	}
	else
	{
		return factorial_Recursion(ans - 1)*ans;
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	int num = 3;
	cout << factorial_Recursion(num) << endl;
	return 0;
}

引自https://www.cnblogs.com/Renyi-Fan/p/6914840.html

posted on 2018-09-11 20:48  泰坦妮克号  阅读(79)  评论(0编辑  收藏  举报