搜索,最后判断一下是否结果为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 ;
}

最新文章

  1. YARN与MRv1的对比
  2. js事件绑定及深入
  3. Oracle 截取字符串
  4. ntc 热敏电阻
  5. 【原】Oracle11gR2图文安装
  6. struts_24_基于XML校验的规则、特点
  7. Spring与其他Web框架集成
  8. [CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex
  9. 【CSS3】---属性选择器
  10. xp snapshot.
  11. 用php逐行读取文件
  12. 不想作死系列---virtualbox最小化安装centos6.5
  13. java框架之springboot
  14. 15树莓派安装图形界面截图工具Shutter
  15. 挖矿病毒 qW3xT.2 最终解决方案
  16. 【Python全栈-后端开发】MySQL数据库-练习题
  17. odoo方法
  18. 利用Red Blob游戏介绍A*算法
  19. jenkins 构建触发器 Poll SCM 和 Build periodically区别
  20. 2018-2019-2 20165114《网络对抗技术》Exp5 MSF基础应用

热门文章

  1. 配置maven默认jdk版本
  2. Oracle使用——oracle表锁住,杀掉锁表进程
  3. cygwin如何下编译安装tmux?
  4. Win32汇编学习(1):基本概念
  5. hihoCoder week16 RMQ-ST算法
  6. Android 设置app 启动
  7. P2504 [HAOI2006]聪明的猴子
  8. URL简单梳理
  9. HDU 4325 Flowers(树状数组+离散化)
  10. [从零开始搭网站四]CentOS配置Tomcat