hdu4403

http://acm.hdu.edu.cn/showproblem.php?pid=4403

Problem 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
/**
hdu4403 暴力搜索
题目大意:给定一个由数字组成的字符串,用一个等号号若干个加号将字符串连接,不许改变字符的顺序,问有多少中方法能够使等式成立
解题思路:题目数据较小,采取用暴力搜索得方法过的。枚举等号的位置进行搜索,复杂度O(2^n)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;

char s[20];
int a[20],b[20],num;
int k1,k2,tt;

void judge(int t,int flag,int sum,int cnt,LL ans)
{
    //printf("(%d %d %d %d %I64d)\n",t,flag,sum,cnt,ans);
    //getchar();
    LL x,y;
    if(flag==0)
    {
        y=cnt;
        x=sum*10+b[t];
    }
    else
    {
        y=sum+cnt;
        x=b[t];
    }
    if(t==k2-1)
    {
        if(x+y==ans)
        {
            ///printf("%I64d %I64d\n",x+y,ans);
            num++;
        }
        return;
    }
    judge(t+1,0,x,y,ans);
    judge(t+1,1,x,y,ans);
}

void dfs(int t,int flag,int sum,int cnt)///第t位,第t位前面是有加号
{
    //printf("%d %d %d %d\n",t,flag,sum,cnt);
    //getchar();
    LL x,y;
    if(flag==0)
    {
        y=cnt;
        x=sum*10+a[t];
    }
    else
    {
        y=sum+cnt;
        x=a[t];
    }
    if(t==k1-1)
    {
      //  printf("%I64d\n",x+y);
        judge(0,0,0,0,x+y);
        return;
    }
    dfs(t+1,0,x,y);
    dfs(t+1,1,x,y);
}

int main()
{
    while(~scanf("%s",s))
    {
        if(s[0]=='E')break;
        int n=strlen(s);
        num=0;
        for(int i=0;i<n-1;i++)
        {
            k1=0,k2=0;
            for(int j=0;j<=i;j++)
                a[k1++]=s[j]-'0';
            for(int j=i+1;j<n;j++)
                b[k2++]=s[j]-'0';
          /*  for(int j=0;j<k1;j++)
                printf("%d ",a[j]);
            printf("\n");
            for(int j=0;j<k2;j++)
                printf("%d ",b[j]);
            printf("\n");*/
            dfs(0,0,0,0);
        }
        printf("%d\n",num);
    }
    return 0;
}
时间: 2024-10-29 18:03:14

hdu4403的相关文章

HDU4403 A very hard Aoshu problem DFS

A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than o

HDU4403(暴搜)

A very hard Aoshu problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4403 Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary m