Problem D. A very hard Aoshu problem (金华赛区 2012 09 22)

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 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 char ch[30];
 9 int num;
10 int len;
11 
12 void dfs(int n)
13 {
14     if(n==len)
15     return;
16     int sum1,sum2;
17     int i,j,k;
18     int t,temp;
19     for(i=0;i<(1<<(n-1));i++)
20     {
21         t=i;
22         sum1=temp=0;
23         for(k=0;k<n;k++)
24         {
25             temp=temp*10+ch[k]-48;
26             if(t&1)
27             {
28                 sum1+=temp;
29                 temp=0;
30             }
31             else if(k+1==n)
32             sum1+=temp;
33             t>>=1;
34         }
35      //   cout<<n<<" "<<i<<" "<<sum1<<endl;
36         for(j=0;j<(1<<(len-n-1));j++)
37         {
38             t=j;
39             sum2=temp=0;
40             for(k=n;k<len;k++)
41             {
42                 temp=temp*10+ch[k]-48;
43                 if(t&1)
44                 {
45                     sum2+=temp;
46                     temp=0;
47                 }
48                 else if(k+1==len)
49                 sum2+=temp;
50                 t>>=1;
51             }
52            // cout<<n<<' '<<j<<' '<<sum2<<endl;
53             if(sum1==sum2)
54             num++;
55         }
56     }
57     dfs(n+1);
58 }
59 int main()
60 {
61     while(~scanf("%s",&ch))
62     {
63         if(!strcmp(ch,"END"))
64         break;
65         num=0;
66         len=strlen(ch);
67         dfs(1);
68         printf("%d\n",num);
69     }
70     return 0;
71 }

你可能感兴趣的:(2012)