Problem C: wjw的hard problem
Time Limit: 1 Sec Memory Limit: 128 MB[ Submit][ Status][ Web Board]
Description
Wjw is very popular among primary school students.
Now give a string of numbers, you need to add a "=" and a number of "+" so that the string is divided into an equation, and both sides are equal, such as the given number is "1212", then it We can form "1 + 2 = 1 + 2" and "12 = 12" two equations, note that the number does not contain 0, and each "+" on both sides must be a number, that is, "1 ++ 1" "++ 2" is illegal. Now wjw would like to know how a number of different tones can be divided into several 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 20. The input ends with a line of "0".
Output
For each test case , output a integer in a line, indicating the number of equations you can get.
Sample Input
Sample Output
【分析】
#include <cstdio>
#include <cstring>
char s[100];
int len,ans;
int f[100][100];
void findd(int deep,int sum,int summ)
{
if(deep==len)
{
if(sum==summ) ans++;
return;
}
for(int i=deep;i<len;i++)
findd(i+1,sum+f[deep][i],summ);
}
void find(int deep,int end,int sum)
{
if(deep==end) findd(end,0,sum);
for(int i=deep;i<end;i++)
find(i+1,end,sum+f[deep][i]);
}
int main()
{
/* FILE *fp1, *fp2;
fp1 = fopen("input03.in","r");
fp2 = fopen("output03.out","w");
*/
while(~scanf("%s",s))
{
if(s[0]=='E') break;
memset(f,0,sizeof(f));
len=strlen(s);
ans=0;
for(int i=0;i<len;i++)
for(int j=i;j<len;j++)
for(int k=i;k<=j;k++)
f[i][j]=f[i][j]*10+s[k]-48;
for(int i=1;i<len;i++)
find(0,i,0);
printf("%d\n",ans);
}
// fclose(fp1);
// fclose(fp2);
return 0;
}