给一个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. Oracle系统视图

    VIEW TABLES 1.DBA_TABLES --查看系统里所有表的信息,只有DBA权限用户才可查看 SELECT * FROM DBA_TABLES WHERE OWNER='HR' AND T ...

  2. 斯坦福 IOS讲义 课件总结 二

    1,OC里面传参数个数不同,可以是两个完全不同的方法.如 - (void)addCard:(Card *)card atTop:(BOOL)atTop; - (void)addCard:(Card * ...

  3. 射频识别技术漫谈(25)——Felica简介

    Felica是SONY公司开发的射频识别技术,该技术使用的载波频率与ISO14443A和ISO14443B一样,都是13.56MHz,所以有人把它称为ISO14443C,但SONY并没有正式接受这样的 ...

  4. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  5. JAVA GUI学习 - JTable表格组件学习_C ***

    /** * JTable高级应用 * @author Wfei * */ public class JTableKnow_C extends JFrame { JTable jTable; MyJMe ...

  6. IPhone多视图切换

    处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法: 而且还附有创建空项目,内存告急处理和动画效果的实现! 具体步骤: 1.创建一个空的项目,然后添加一个ViewContro ...

  7. sqlite性能简单測试

    主要測试sqlite在大数据量下的插入及查询性能: 測试环境:Centos6.4  1G内存  单核 数据量 大小 索引字段检索(耗时) 非索引字段检索(耗时) 总插入时间 10W 19M 0.001 ...

  8. 【企业库6】【日志应用程序块】实验2:创建和使用异步Trace Listener

    Lab 2: Create and Use an Asynchronous Trace Listener 实验2:创建和使用异步Trace Listener In this lab, you will ...

  9. TCP粘包和半包的处理方法

    先把处理的方法的代码放这里: 解析数据帧的代码: bool CSocket::findData(byte* buff, int& len) { for (int i = 0; i <= ...

  10. Collection类学习笔记

    binarySearch原理: public static index halfSearch(List<String> list, String key) { int max,min,mi ...