给一个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. Linux系统之UpStart

    子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...

  2. 在Linux中创建静态库和动态库 (转)

    我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态 库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而 ...

  3. COB封装的优势

    随着固态照明技术的不断进步,COB(chip-on-board)封装技术得到越来越多的重视,由于COB光源有热阻低,光通量密度高,眩光少,发光均匀等特性,在室内外照明灯具中得到了广泛的应用,如筒灯,球 ...

  4. 盛希泰:办公室就像男人的春药——人的一生的精力是有限的,你把有限的时间分配给谁决定你的成败——你有N多选择,你人生的积累就是N多选择加起来的结果

    欢迎关注“创事记”的微信订阅号:sinachuangshiji 创事记注:12月22日晚上,盛希泰在清华大学旧经管报告厅面对清华师生讲了一堂<创业引导课>.本文由洪泰帮根据课堂录音整理完成 ...

  5. HDU 2852 KiKi's K-Number

    权值线段树 #include <cstdio> #include <cstring> const int N=200000,M=220000; int k,q,x,y,sum[ ...

  6. Sicily-1438

    一.      题意 买二送一.排序之后隔三求和,求折扣的最大值. 二.      代码 // // main.cpp // sicily-1438 // // Created by ashley o ...

  7. One Person Game(扩展欧几里德求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  8. U3D navmesh寻路简单示范

    要求:放置一个BOSS,创建几个路标,自动循环这几个路标形成回路,变成自动巡逻,并配合animator系统的控制开关控制BOSS的动作 1.先设置好BOSS 中animator的控制开关,只看Run和 ...

  9. 【转】Nginx+Tomcat搭建高性能负载均衡集群

    最近对负载均衡比较感兴趣,研究了公司的负载均衡的配置,用的是阿里的SLB,相当于不用运维,只需要在后台进行简单的配置就能完成Tomcat的负载均衡,索性在网上找了几篇文章去尝试搭建一个集群,然而很多都 ...

  10. 在SAE上搭建自定义版本WordPress, 并用SAE Storage代替WordPress Uploads

    问题由来: 1. SAE中默认的WP for SAE版本太低, 导致某些基于新版本WordPress的插件不能使用. 2. SAE中单个APP的代码空间仅有100M. 将WordPress中uploa ...