Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.
123456789 3 21 1
18 1
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
LL n,ans,l;
char a[20];
void dfs(LL len,LL sum)
{
if(len==l)
{
if(sum==n)
ans++;
return ;
}
LL i,s=0;
for(i=len;i<l;i++)
{
s=s*10+a[i]-'0';
dfs(i+1,sum+s);
if(len!=0)
dfs(i+1,sum-s);
}
}
int main()
{
while(~scanf("%s %lld",a,&n))
{
ans=0;
l=strlen(a);
dfs(0,0);
printf("%lld\n",ans);
}
return 0;
}