欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

例4.6 - 数的计数 - 存量递归法

程序员文章站 2022-07-16 21:15:27
...
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<typeinfo>
#include<sstream>
#include<fstream>
#include<algorithm>
using namespace std;

int h[101];	//存量思维 
int n=0;

void coutN(int n){	//求h[n] 
	if(h[n] != -1) return;	//以前存量了,直接byebye
	
	h[n]=1;	//先设置本身+1 
	for(int i=1;i<=n/2;i++){	//只看leftest, 6->36与3完全相同	->忽略后面的6 
		coutN(i);	//不算h[i],怎么+呢?? 
		h[n] += h[i]; 
	} 
}

int main()
{
	//流程图!! 

	cout << "输入:";
	cin >> n;
	memset(h,-1,sizeof(h));	//设置-1可以用memset 
	
	coutN(n);	//开始计数 
	cout << "总数:" << h[n]; 
	
	return 0; 
}
//cout << "输入:";

结果:

例4.6 - 数的计数 - 存量递归法