首页 技术 正文
技术 2022年11月14日
0 收藏 466 点赞 2,792 浏览 995 个字

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.

InputEach case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.OutputThe output contains one line for each data set : the number of ways you can find to make the equation.Sample Input

123456789 3
21 1

Sample Output

18
1题目大意就是 在一个在来两个数字之间添加+ — 或者不做处理 使他可以得到后一个数,并记录次数就好了
思路:DFS遍历每一种情况
#include<iostream>
#include<string >
using namespace std;
string a;
typedef long long ll;
ll n,ans;
void dfs(int row,int sum){
if(row==a.size())
{
if(sum==n)
ans++;
return ;
} ll t=;
for(int i=row;i<a.size();i++)
{
t=t*+a[i]-'';
dfs(i+,sum+t);
if(row==) continue ; //因为当row==0时 sum=0,如过加减号的话相当于在第一个数前边加负号,所以要跳过
     dfs(i+,sum-t);
}
}int main()
{
while(cin>>a>>n){
ans=;
dfs(,);
cout<<ans<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726