题意:给定一串数字字符串,和一个数字n,从串中任意两相邻的字符间加上+或-,求有多少中情况能使得结果等于n
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2266
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#define LL long long
using namespace std;
const int maxn = 15;
char ch[maxn];
LL ans,n;
int len;
void dfs(int k,int sum)
{
if(k==len)
{
if(sum==n)ans++;
return;
}
LL x = 0;
for(int i=k;i<len;i++)
{
x = x*10 + (ch[i]-'0');
dfs(i+1,sum+x);
if(k!=0)
dfs(i+1,sum-x);
}
}
int main()
{
while(scanf("%s%lld",ch,&n)!=EOF)
{
ans=0;
len = strlen(ch);
dfs(0,0);
printf("%lld\n",ans);
}
}
转载请注明原文地址: https://ju.6miu.com/read-33760.html