p1473 Zero Sum
2024-09-24 16:32:50
搜索,最后判断一下是否结果为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 ;
}
最新文章
- YARN与MRv1的对比
- js事件绑定及深入
- Oracle 截取字符串
- ntc 热敏电阻
- 【原】Oracle11gR2图文安装
- struts_24_基于XML校验的规则、特点
- Spring与其他Web框架集成
- [CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex
- 【CSS3】---属性选择器
- xp snapshot.
- 用php逐行读取文件
- 不想作死系列---virtualbox最小化安装centos6.5
- java框架之springboot
- 15树莓派安装图形界面截图工具Shutter
- 挖矿病毒 qW3xT.2 最终解决方案
- 【Python全栈-后端开发】MySQL数据库-练习题
- odoo方法
- 利用Red Blob游戏介绍A*算法
- jenkins 构建触发器 Poll SCM 和 Build periodically区别
- 2018-2019-2 20165114《网络对抗技术》Exp5 MSF基础应用