【问题】
李白壶中初始2斗酒,遇花喝1斗,遇店加1倍,几种方案?
【解析】
题目中有十次花,五次店,因为最后一次之后酒为0,故最后一次应该遇到花。
运用递归的思想,每次都具备两种可能性,遇花或者遇店。终止条件:遇到店后,最后一次遇到花时酒还剩一斗。
#include<iostream>
using namespace std;
int res = 0;
void f(int a, int b, int c)
{
if (a == 0 && b == 1 && c == 1)
{
res++;
}
if (a > 0)
f(a - 1, b, c * 2);
if (b > 0)
f(a, b - 1, c - 1);
}
int main()
{
int hua = 10, dian = 5, sum = 2;
f(dian, hua, sum);
cout << res;
return 0;
}