蓝桥杯 历届试题 李白打酒
2017-04-07 11:42
232 查看
标题:李白打酒
递归,注意一点,最后一次遇到的 一定是花(最后没有酒)
看到一个别人写的可以输出所有的情况,感觉这个比较容易学习http://www.tuicool.com/articles/yUJzIz
话说大诗人李白,一生好饮。幸好他从不开车。 一天,他提着酒壶,从家里出来,酒壶中有酒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; }
相关文章推荐
- 蓝桥杯 历届试题 李白打酒
- 蓝桥杯 历届试题 回文数字 (暴力)
- 蓝桥杯 -- 历届试题 分糖果 【模拟,题意是关键】
- 蓝桥杯 历届试题 最大子阵
- 蓝桥杯 历届试题 翻硬币
- 蓝桥杯 历届试题 合根植物 JAVA
- 蓝桥杯--历届试题 蚂蚁感冒 【思维】
- 蓝桥杯 历届试题 大臣的旅费 (两次dfs 之树上求最长路径)
- 蓝桥杯历届试题——九宫重排(bfs)
- 蓝桥杯历届试题——九宫重排(启发式搜索)
- 蓝桥杯 历届试题 连号区间数
- 蓝桥杯 历届试题 有理数类
- 蓝桥杯 历届试题 兰顿蚂蚁(简单模拟)
- 蓝桥杯 历届试题 打印十字图(打印)
- 历届试题 兰顿蚂蚁 ——蓝桥杯
- 蓝桥杯 历届试题 分糖果
- [蓝桥杯历届试题] 欧拉与鸡蛋
- [蓝桥杯历届试题] 第39级台阶
- 蓝桥杯 历届试题 幸运数
- 蓝桥杯-- 历届试题 大臣的旅费(树的直径)