UASCO Zero Sum DFS + Stack
给一个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的更多相关文章
- 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 ...
- 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 ...
- poj3984迷宫问题(dfs+stack)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35426 Accepted: 20088 Descriptio ...
- 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 ...
- LeetCode Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 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 ...
- POJ2735/Gym 100650E Reliable Nets dfs
Problem E: Reliable NetsYou’re in charge of designing a campus network between buildings and are ver ...
- BZOJ2783: [JLOI2012]树 dfs+set
2783: [JLOI2012]树 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 588 Solved: 347 Description 数列 提交文 ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
随机推荐
- python安装集成包
anaconda, 包含各种科学运算包以及astropy.装完它一劳永逸. https://www.continuum.io/downloads
- [LeetCode]题解(python):096-Unique Binary Search Trees
题目来源: https://leetcode.com/problems/unique-binary-search-trees/ 题意分析: 给定一个整数n,返回所有中序遍历是1到n的树的可能. 题目思 ...
- DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间
dll 与 exe 之间的通讯方式有很多种, 本文采用回调函数的方法实现, 本文也将研究多线程,多模块的情况下,回调函数所在的线程, 啥也不说了,先附上代码: 下面的是dll模块的的, dll的工程文 ...
- Dating with girls(1)(二分+map+set)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- [译]Stairway to Integration Services Level 7 - SSIS 工作流管理中级
介绍 前一个章节我们对SSIS的优先约束做了初步了解,并且实验了MaxConcurrentExecutables 包属性的设置. 本文将测试“On Success”, “On Completion”, ...
- 在VS2010上使用C#调用非托管C++生成的DLL文件
背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用.本篇文章将引导你快速理解这个调用 ...
- Oracle语句优化规则(一)
1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种: a. RULE (基于规则) b. COST (基于成本) c. CHOOSE (选择性) 设置缺省的优化 ...
- hdu 4782 Beautiful Soupz
模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...
- eval以及json
参考 http://www.cnblogs.com/artwl/archive/2011/09/07/2169680.html http://www.cnblogs.com/objectorl/arc ...
- 转: requirejs压缩打包r.js使用示例 2 (~~很详细的教程)
这一篇来认识下打包工具的paths参数,在入门一中 就介绍了require.config方法的paths参数.用来配置jquery模块的文件名(jQuery作为AMD模块时id为“jquery”, 但 ...