搜索,最后判断一下是否结果为0就行。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF,mod=;
char arr[]={'+','-',' '};
vector<string> res; int cal(string str)
{
int pos=;
pos=str.find_first_of("+-",pos);
if(pos==-)return ;
int ope1=,ope2=;
for(int i=;i<pos;++i)
{
if(str[i]!=' ')
{
ope1=ope1*+str[i]-'';
}
}
int oldpos=++pos;
for(;;)
{
pos=str.find_first_of("+-",oldpos);
if(pos==-)pos=str.size();
for(int i=oldpos;i<pos;++i)
{
if(str[i]!=' ')
{
ope2=ope2*+str[i]-'';
}
}
if(str[oldpos-]=='+')ope1=ope1+ope2,ope2=;
else ope1=ope1-ope2,ope2=;
oldpos=pos+;
if(pos==str.size())break;
}
return ope1;
} void dfs(int pos,int n,string &str)
{
if(pos==n+)
{
if(cal(str)==)
{
res.push_back(str);
}
return;
}
for(int i=;i<;++i)
{
str+=arr[i];
str+=pos+'';
dfs(pos+,n,str);
str.erase(str.end()-,str.end());
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//for(lon time=1;time<=casenum;++time)
{
int n;
cin>>n;
string str="";
dfs(,n,str);
sort(res.begin(),res.end());
for(int i=;i<res.size();++i)
{
cout<<res[i]<<endl;
}
}
return ;
}

p1473 Zero Sum的更多相关文章

  1. 洛谷P1473 零的数列 Zero Sum

    P1473 零的数列 Zero Sum 134通过 170提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 路过的一定帮我看错了我死了- 题目描述 请考虑 ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. java调用ws服务

    1.找到ws服务地址,例如:http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx 2.新建项目 3.进入命令行窗口,进入当前项目src目录下,然 ...

  2. 设置Eclipse具有字母自动联想

    Window->Preferences->Java->Editor->ContentAssist(内容助手)里面的Enable auto activation里面第二行再加上a ...

  3. topcoder srm 683 div1

    problem1 link 肯定存在相邻两堆满足不会存在任何操作在这两堆之间进行.然后就成为一条链,那么只需要维护链的前缀和即可判断当前堆和前一堆之间需要多少次操作. problem2 link 对于 ...

  4. sqlserver无法在数据库上放置锁

    由于无法在数据库 ' ' 上放置锁,ALTER DATABASE 失败.请稍后再试.消息5069,级别16,状态1,第一行ALTER DATABASE 语句失败. 解决方法: 新建查询,通过下面SQL ...

  5. debian下如何源码安装tmux

    一.源码安装ncurses库 1.1 获取源码 wget https://invisible-island.net/datafiles/release/ncurses.tar.gz tar xvf n ...

  6. 如何查看linux程序被何种版本的编译器编译的?

    答: 使用vi工具之间搜索关键字"GCC"即可找出编译该程序的编译器版本号!

  7. 什么是TF-A?

    1. TF-A的全称是什么? Arm Trusted Firmware 2. TF-A的作用是什么? 在secure world和non-secure world之间切换 3. TF-A涉及到哪几个部 ...

  8. linux内核中的hisi_sas是什么?

    答: 是一个HISILICON SAS 控制器驱动(HISILICON SAS controller driver)

  9. MySQL 日期时间函数

    目录 datetime和timestamp区别: timestamp类型字段特殊性: Mysql获取日期时间函数: now() curdate() curtime() Extract() last_d ...

  10. 轻重搭配|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采 ...