给一个N 表示1 2 3 ...N

求出所有 zero sum的情况

【简单Dfs 即可】 运算结果的时候我使用了一个stack...

比如N = 7

那么要求输出

1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7 Source Code:
/*
ID: wushuai2
PROG: zerosum
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)
#define RV(num) ((num) > 0 ? 0 : 1) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; ofstream fout ("zerosum.out");
ifstream fin ("zerosum.in"); int N, b[];
void solve(){
int i, j, t;
vector <int> vec;
vec.push_back();
for(i = ; i <= N; ++i){
int num = vec[vec.size() - ];
if(num < ){
num = vec[vec.size() - ];
}
if(b[i] == ){
vec.pop_back();
vec.push_back(num * + i);
} else if(b[i] == ){
vec.push_back(-);
vec.push_back(i);
} else if(b[i] == ){
vec.push_back(-);
vec.push_back(i);
} else{
vec.push_back(i);
}
} int ans = vec[];
for(i = ; i < vec.size(); i += ){
if(vec[i] == -){
ans += vec[i + ];
} else if(vec[i] == -){
ans -= vec[i + ];
}
}
if(ans == ){
fout << ;
for(i = ; i <= N; ++i){
if(b[i] == ) fout << ' ';
else if(b[i] == ) fout << '+';
else if(b[i] == ) fout << '-';
fout << i;
}
fout << endl;
}
} void change(int n){
int i, j;
if(n == N + ){
solve();
return;
}
for(i = ; i <= ; ++i){
b[n] = i;
change(n + );
}
} int main() {
int i, j, k, l, m, n, t, s, c, w, q, num;
fin >> N;
for(i = ; i <= N; ++i){
b[i] = ;
}
change(); fin.close();
fout.close();
return ;
}

UASCO Zero Sum DFS + Stack的更多相关文章

  1. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  2. LeetCode-494. Target Sum(DFS&DP)

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  3. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  4. Minimum Path Sum(DFS,DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. LeetCode Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  6. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  7. POJ2735/Gym 100650E Reliable Nets dfs

    Problem E: Reliable NetsYou’re in charge of designing a campus network between buildings and are ver ...

  8. BZOJ2783: [JLOI2012]树 dfs+set

    2783: [JLOI2012]树 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 588  Solved: 347 Description 数列 提交文 ...

  9. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

随机推荐

  1. Python 爬取CSDN博客频道

    初次接触python,写的很简单,开发工具PyCharm,python 3.4很方便 python 部分模块安装时需要其他的附属模块之类的,可以先 pip install wheel 然后可以直接下载 ...

  2. python自学笔记(五)python文本操作

    一.python自带方法 r:read 读 w:write 写 a:append 尾行追加 先命令行进入python后 >>>d = open('a.txt','w') #在对应路径 ...

  3. selenium 学习笔记 ---新手学习记录(10) 问题总结(java)--poi--excel 操作

    1.当我使用poi想要操作xlsx时,报错,解决方法下载xmlbeans-2.6.0.jar架包,引入即可.点我下载 https://yunpan.cn/cSdVqp6e4v6Jk  访问密码 2c5 ...

  4. js获取浏览器窗口的大小

    在我本地测试当中: 在IE.FireFox.Opera下都可以使用 document.body.clientWidth document.body.clientHeight 即可获得,很简单,很方便. ...

  5. html文件中文在浏览器中显示乱码问题解决

    利用浏览器打开html文件时,中文显示乱码,如下是原文件的内容 1 <html>   2         <head>   3             <title> ...

  6. Oracle中sign函数和decode函数的使用

    Oracle中sign函数和decode函数的使用 1.比较大小函数SIGN sign(x)或者Sign(x)叫做 符号函数,其功能是取某个数的符号(正或负): 当x>0,sign(x)=1; ...

  7. android项目 之 记事本(6)----- 加入手写

    想必大家都用过QQ的白板功能,里面主要有两项,一个是涂鸦功能,事实上类似于上节的画板功能,而还有一个就是手写,那记事本怎么能没有这个功能呢,今天就来为我们的记事本加入手写功能. 先上图,看看效果: 看 ...

  8. SQL Server 性能优化

    今天有位网友找我给他原有的系统数据库优化下查询速度,个人总结了几点对sqlserver的优化 1.Sql查询语句的优化,如:能使用外连接查询出来的尽量别用内连接...,这些个就不废话,如果我使用这个给 ...

  9. bootstrap固定响应式导航

    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap. ...

  10. 在ASP.NET中动态加载内容(用户控件和模板)

    在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...