[One question per day] -20180806 [Li Bai hitting wine] (not perfect)

topic:

It is said that the great poet Li Bai has a good drink all his life. Fortunately, he never drives.
One day, he came out of the house carrying a hip flask. There were 2 buckets of wine in the flask. He sang as he
walked : Walk on the street without incident, pick up the pot to drink.
Double at every store, drink a bucket when you meet flowers.
Along the way, he encountered the store 5 times and spent 10 times. Knowing that the last time he met was the flower, he just drank the wine.
Please calculate the order of Li Bai's encounter with the store and the flower. You can record the encounter with shop as a and the encounter with flowers as b. Then: babaabbabbabbbb is a reasonable order. How many answers are there in total like this? Please calculate the number of all possible solutions (including those given by the title).

Ideas:

1. The call of recursive function;
2. The recursive exit.

Question answer:
#include<iostream>
#define SHOP 1              //定义遇店
#define FLOWER 0          //定义遇花
#define Ori_amount 2     //原始酒量

int Ans_nums = 0;        //定义全局变量记录满足条件的方案个数
using namespace std;
/**
 *ToDo:函数判定喝酒打酒
 *@prarm a:遇店           drinking_amount += drinking_amount;
 *@prarm b:遇花           drinking_amount -= 1
 *@prarm drinking_amount:在进行本轮操作之前的酒的量
 */
void LiBaiSee(int a,int b,int drinking_amount);
int main()
{
    LiBaiSee(5,10,Ori_amount);
    cout << "共有方案:" <<  Ans_nums << "种";
    return 0;
}
void LiBaiSee(int a,int b,int drinking_amount)
{
    if(a < 0 || b < 0 || drinking_amount < 0)
        return;                         //不满足条件遍历结束
    if(a == 0 && b == 1 && drinking_amount == 1)   //已知最后一次遇到花
    {
        Ans_nums++;             //符合最终条件方案数目+1
        return;
    }
    LiBaiSee(a - 1,b,2 * drinking_amount);            //逢店加一倍
    LiBaiSee(a,b - 1,drinking_amount - 1);           //遇花喝一斗
}
My extension:

Extended Content: output program list
idea: to find the corresponding recursive exports

Code:
暂时无法扩展:等后面学习
reward:

The understanding of the use of recursion: if there is a problem, it is completed by multiple repetitive operations and there is a certain connection between the previous and subsequent operations. Use recursion.
[Reading content] (unfinished)

Guess you like

Origin blog.csdn.net/Chaoyuan_Jam/article/details/81462250