zoukankan      html  css  js  c++  java
  • HDU4403 A very hard Aoshu problem

    搜索提,等号只有一个可以剪枝

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 char seq[50];
     7 char str[50];
     8 int n,ans,eq;
     9 bool ok(int len)
    10 {
    11     long long a,sum=0;
    12     int i,flg=1;
    13     queue<char> q;
    14     q.push('+');
    15     seq[len]='\0';
    16     for(i=0; i<len; i++)
    17     {
    18         a=0;
    19         while(i<=len&&seq[i]>='0'&&seq[i]<='9') a=a*10+seq[i]-'0',i++;
    20         q.push(seq[i]);
    21         if(flg&&q.front()=='+')
    22             sum+=a;
    23         else
    24             flg=0,sum-=a;
    25         q.pop();
    26     }
    27     if(sum==0) return 1;
    28     else return 0;
    29 }
    30 void dfs(int cur,int q)
    31 {
    32     if(eq>1) return;
    33     if(q>n)
    34     {
    35         if(ok(cur)) ans++;
    36         return;
    37     }
    38     seq[cur]=str[q];
    39     dfs(cur+1,q+1);
    40     seq[cur]='+';
    41     seq[cur+1]=str[q];
    42     dfs(cur+2,q+1);
    43     seq[cur]='=';
    44     seq[cur+1]=str[q];
    45     eq++;
    46     dfs(cur+2,q+1);
    47     eq--;
    48 }
    49 int main()
    50 {
    51     while(cin>>str,str[0]!='E')
    52     {
    53         ans=0;eq=0;
    54         seq[0]=str[0];
    55         n=strlen(str)-1;
    56         dfs(1,1);
    57         cout<<ans<<endl;
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    [C++]2-5 分数化小数
    [C++]2-4 子序列的和
    [C++]2-3 倒三角形
    [C++]2-2 韩信点兵
    [C++]2-1 水仙花数
    [C++]竞赛模板·数据统计与IO(重定向版与非重定向版)
    数学建模·经验小结
    信息检索·论文写作
    PPT制作
    演讲与语言表达
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2698512.html
Copyright © 2011-2022 走看看