p1473 Zero Sum
搜索,最后判断一下是否结果为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的更多相关文章
- 洛谷P1473 零的数列 Zero Sum
P1473 零的数列 Zero Sum 134通过 170提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 路过的一定帮我看错了我死了- 题目描述 请考虑 ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Linux 双向 SSH 免密登录
原理 双向,顾名思义,双方互通,此处的意思是多台 linux 两两免密登录.双向比单向多了些操作,单向只需把某一个linux的公钥发送给其他linux即可,而双向要实现集群中的每一台机器都保存其他所有 ...
- #2718. 「NOI2018」归程 kruskal重构树
链接 https://loj.ac/problem/2718 思路 我们希望x所在的连通块尽量的大,而且尽量走高处 离线的话可以询问排序,kruskal过程中更新答案 在线就要用kruskal重构树 ...
- 2870: 最长道路tree
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2870 思路 先把树转化为二叉树 再链分治 %%yyb 代码 #include <ios ...
- 黄金连分数|2013年蓝桥杯B组题解析第四题-fishers
黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜,它首次 ...
- 为静态博客生成器WDTP移植了一款美美哒主题
前言 关于这个主题的移植后公布,我已经联系了主题作者并取得同意,这个主题是一夜涕所写的Sgreen,预览图见下 关于WDTP 就是一个很方便很便携很快速的cpp编写的带gui跨平台的开源的静态博客生成 ...
- nowcoder 合并回文子串
链接:https://www.nowcoder.com/acm/contest/6/C来源:牛客网题目输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc&q ...
- Vue.extend构造器和$mount实例构造组件后可以用$destroy()进行卸载,$forceUpdate()进行更新,$nextTick()数据修改
html <div id="app"> </div> <p><button onclick="destroy()"&g ...
- BZOJ 4771 七彩树(可持久化线段树合并)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=4771 思路 和 HDU 3333 其实有点像,不过是把序列的问题放在了树上,多维护一个深度即 ...
- 在C#中理解和实现策略模式的绝对入门教程
介绍 本文的目的是理解战略模式的基础知识,并试图了解何时可以使用,并有一个基本的实现,以便更好地理解.在现实世界的应用中,这是无法实施战略模式的,所采用的例子也远没有实际可行.这篇文章的想法只是为了说 ...
- HDU 4403 A very hard Aoshu problem(dfs爆搜)
http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...