李白喝酒问题递归求解

在这里插入图片描述
c++源码

#include <iostream>
using namespace std;
int count1 = 0;
char a[15];
void fun(int store, int flower, int wine, int i)
{
	if ((store > 5) || (flower > 10))
		return;
	else
		if ((store == 5) && (flower == 10) && (i == 15))
			if (a[14] == 'b' && wine == 0)  count1++;
	a[i] = 'a';
	fun( store + 1, flower, wine * 2,i + 1);
	a[i] = 'b';
	fun( store, flower + 1, wine - 1,i+1);
}
int main(){
	fun(0, 0, 2, 0);
	cout << "方案个数:"<<count1;
	return 0;
}
发布了2 篇原创文章 · 获赞 0 · 访问量 47

猜你喜欢

转载自blog.csdn.net/qq_40465032/article/details/105180755