第五届蓝桥杯c/c++B组3

题目三:李白打酒


标题:李白打酒

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

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

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

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

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

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

#include<iostream>
#include<cmath>
using namespace std;
int main() {
  int arr[16], x, y, s=0, v;
  for (arr[0]=0; arr[0]<=1; arr[0]++)
   for (arr[1]=0; arr[1]<=1; arr[1]++)
    for (arr[2]=0; arr[2]<=1; arr[2]++)
     for (arr[3]=0; arr[3]<=1; arr[3]++)
      for (arr[4]=0; arr[4]<=1; arr[4]++)
       for (arr[5]=0; arr[5]<=1; arr[5]++)
        for (arr[6]=0; arr[6]<=1; arr[6]++)
         for (arr[7]=0; arr[7]<=1; arr[7]++)
          for (arr[8]=0; arr[8]<=1; arr[8]++)
           for (arr[9]=0; arr[9]<=1; arr[9]++)
            for (arr[10]=0; arr[10]<=1; arr[10]++) 
             for (arr[11]=0; arr[11]<=1; arr[11]++)
              for (arr[12]=0; arr[12]<=1; arr[12]++)
               for (arr[13]=0; arr[13]<=1; arr[13]++){
                 v=2, x=0, y=0;
                 for (int i=0; i<14; i++){
                   if (arr[i]==0){
                      x++;
                      v=v+v;
                      //cout<<x<<endl;
                   }
                   if (arr[i]==1){
                     y++;
                     v-=1;
                     //cout<<v<<endl;
                   }
                   if (x==5&&y==9&&v==1)
                   s++;   
                 }
               }
  cout<<s;
}

 

#include<iostream>
using namespace std;
int arr[20], s=0;;
void dfs(int a, int b, int x, int y){
  if (b==15){
    if (x==5&&y==9&&a==1){
      s++;
      for (int i=0; i<15; i++)
        cout<<arr[i]<<" "; 
      cout<<endl;
    }
  }
  else{
    arr[b]=1;
    dfs(a*2, b+1, x+1, y);
    arr[b]=0;
    dfs(a-1, b+1, x, y+1);
  }
}
int main() {
  dfs(2, 1, 0, 0);
  cout<<s;
}

  

posted @ 2017-03-18 12:47  白丁一枚  阅读(124)  评论(0编辑  收藏  举报