zoukankan      html  css  js  c++  java
  • HDU4403(暴搜)

    A very hard Aoshu problem

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students: 

    Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations. 

    Input

    There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END". 

    Output

    For each test case , output a integer in a line, indicating the number of equations you can get. 

    Sample Input

    1212
    12345666
    1235
    END

    Sample Output

    2
    2
    0

     1 //2016.8.20
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #define N 20
     6 using namespace std;
     7 
     8 int ans, num[N][N], n;//num[i][j]用来记录从i到j位,数字的大小。
     9 char s[N];
    10 
    11 void dfsR(int pos, int leftsum, int rightsum)
    12 {
    13     if(pos == n && leftsum == rightsum){
    14         ans++; return;
    15     }
    16     if(leftsum < rightsum)return;//剪枝
    17     for(int i = pos; i < n; i++)
    18           dfsR(i+1, leftsum, rightsum+num[pos][i]);
    19 }
    20 
    21 void dfsL(int pos, int sum, int mid)//pos表示当前处理的位置,sum表示已处理的和,mid表示等号的位置。
    22 {
    23     if(pos == mid)//对左边dfs完后,对右边dfs
    24           dfsR(mid, sum, 0);
    25     for(int i = pos; i < mid; i++)//枚举加号位置。
    26           dfsL(i+1, sum+num[pos][i], mid);
    27 }
    28 int main()
    29 {
    30     while(scanf("%s", s)!=EOF)
    31     {
    32         if(s[0] == 'E')break;
    33         ans = 0;
    34         n = strlen(s);
    35         for(int i = 0; i < n; i++)
    36         {
    37             int tmp = 0;
    38             for(int j = i; j < n; j++)
    39             {
    40                 tmp += s[j]-'0';
    41                 num[i][j] = tmp;
    42                 tmp *= 10;
    43             }
    44         }
    45         for(int i = 1; i < n; i++)//枚举等号的位置,然后暴搜
    46               dfsL(0, 0, i);
    47         cout<<ans<<endl;
    48     }
    49 
    50     return 0;
    51 }
  • 相关阅读:
    Thinkph5——模型软删除
    ThinkPHP5——URL参数带中文,报"模块不存在"
    ThinkPHP5多模板配置二级域名
    使用Composer安装阿里云短信失败
    GIT快速入门
    Python全栈之路系列----之-----面向对象4(接口与抽象,多继承与多态)
    Python全栈之路系列----之-----面向对象3(继承与派生)
    Python全栈之路系列----之-----面向对象1(初识)
    Python全栈之路系列----之-----异常处理
    Python全栈之路系列----之-----内置函数和匿名函数lamdba
  • 原文地址:https://www.cnblogs.com/Penn000/p/5790057.html
Copyright © 2011-2022 走看看