给一个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. selenium 学习笔记 ---新手学习记录(3) 问题总结(java)

    1.验证码简单处理 /** * 验证码等待输入函数 * */ private void ZcYzm(WebDriver driver){ boolean flag=false; while(flag= ...

  2. C-重定向

    说实话,第一次接触重定向这一个概念,感觉是那么的神奇简洁不可思议…………………… freopen() 本来应该是打开的是文件指针,但是分配了指针,使她(亲切)指向了标准输入.输出.错误流. 用 法: ...

  3. 斯坦福 IOS讲义 课件总结 三

    1,@property (nonatomic,readwrite)NSInteger score;注意这里有一个只读和只写的属性,readonly. 2,重写初始化方法也可以改名字和传参数,(改名一般 ...

  4. Apache proxy中转设置

    参考http://sjsky.iteye.com/blog/1067119 打开http.conf  (macOS中 Apache配置文件在/etc/apache2/中   etc是隐藏的) 确保下面 ...

  5. IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案)

    原文 IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案) 对于IIS网站,大伙用的比较多,就不啰嗦了.   今天和说说大伙比较少使用的"IIS应用程序”和虚拟目录的区别 ...

  6. android天气查询(二)之网络json数据的获取

    前面一篇文章介绍了如何使用ksoap获取天气信息,但是使用的网络资源受到了限制,所以我们这里会采用第二种方法,可以无限制的获取.http://m.weather.com.cn/data/1010101 ...

  7. 【C++学习笔记】继承与派生基础概念

    面向对象的程序设计主要有四个特点:抽象.封装.继承和多态.其中继承是我认为最最重要的一个特性,可以说继承是面向对象的精华所在. 举一个继承的浅显易懂的例子:假如我们已经有了一个“马”的类,其中成员变量 ...

  8. 【在网页中获取截图数据】Chrome和Firefox下的实战经验

    [转载自我在segmentfault的专栏:https://segmentfault.com/a/1190000004584071] 最近在实现一个功能,需求如下: 前提:当前页面无弹窗 页面任意位置 ...

  9. TreeView控件绑定数据库

    1.在设计视图里面的代码 <form id="form1" runat="server"> <div> <h1>两个表< ...

  10. 我用的比较少的CSS选择器

    选择器 描述 [attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性和值的元素. [attribute~=value] 用于选取属性值中包含指定 ...