您的位置:首页 > 其它

蓝桥杯 历届试题 李白打酒

2017-04-07 11:42 232 查看
标题:李白打酒

话说大诗人李白,一生好饮。幸好他从不开车。

一天,他提着酒壶,从家里出来,酒壶中有酒2斗。他边走边唱:

无事街上走,提壶去打酒。
逢店加一倍,遇花喝一斗。

这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。

请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。

注意:通过浏览器提交答案。答案是个整数。不要书写任何多余的内容。


递归,注意一点,最后一次遇到的 一定是花(最后没有酒)

#include <iostream>
#include <stdio.h>
using namespace std;

int counter=0;
void work(int step,int jiu,int dian,int hua)
{
if(step<1||jiu<1||dian<0||hua<1)
{
return;
}
if(step==1&&jiu==1&&dian==0&&hua==1)//14步的时候遇到花
{
counter++;
return;
}
work(step-1,jiu-1,dian,hua-1);//遇到花
work(step-1,jiu*2,dian-1,hua);//遇到店

}
int main(int argc, char** argv) {
work(15,2,5,10);
cout<<counter<<endl;
return 0;
}


看到一个别人写的可以输出所有的情况,感觉这个比较容易学习http://www.tuicool.com/articles/yUJzIz

#include<iostream>
using namespace std;
int count = 0;
char a[15];
void show(char *a,int num)
{
int i=0;
while(i<num)cout<<a[i++];
cout<<endl;
}
void fun(int i,int store,int flower,int wine)
{
if(store>5||flower>10)return;
if(store==5&&flower==10&&i==15)
{
if(a[14]=='b'&&wine==0)
{
count++;
show(a,15);
}
return;
}
a[i] = 'a';
fun(i+1,store+1,flower,wine*2); //逢店加一倍
a[i] = 'b';
fun(i+1,store,flower+1,wine-1); //遇花喝一斗
}
int main()
{
fun(0,0,0,2);
cout<<count<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: