USACO Section 2.3: Zero Sum
这题我做得比较麻烦,网上有个比较简单的程序。
/*
ID: yingzho1
LANG: C++
TASK: zerosum
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
ifstream fin("zerosum.in");
ofstream fout("zerosum.out");
int N;
set<string> res;
string intToString(int n) {
string ret;
) ";
while (n) {
);
ret += tmp;
n /= ;
}
reverse(ret.begin(), ret.end());
string res;
res += ret[];
; i < ret.size(); i++) res = res + ' ' + ret[i];
return res;
}
bool check(vector<int> &num, vector<char> &op) {
) return false;
];
; i < num.size(); i++) {
] == '+') sum += num[i];
else sum -= num[i];
}
;
}
void dfs(int dep, vector<int> &num, vector<char> &op, int sum) {
if (dep > N) {
/*for (int i = 0; i < num.size(); i++) cout << num[i] << " ";
cout << endl;
for (int i = 0; i < op.size(); i++) cout << op[i] << " ";
cout << endl;*/
if (sum) num.push_back(sum);
if (check(num, op)) {
string tmp;
; i < num.size()-; i++) {
tmp = tmp + intToString(num[i]) + op[i];
}
tmp = tmp + intToString(num[num.size()-]);
res.insert(tmp);
}
if (sum) num.pop_back();
return;
}
sum = sum* + dep;
dfs(dep+, num, op, sum);
num.push_back(sum);
op.push_back('+');
dfs(dep+, num, op, );
op.pop_back();
op.push_back('-');
dfs(dep+, num, op, );
op.pop_back();
num.pop_back();
}
int main()
{
fin >> N;
vector<int> num;
vector<char> op;
dfs(, num, op, );
for (set<string>::iterator it = res.begin(); it != res.end(); it++) {
fout << *it << endl;
}
;
}
USACO Section 2.3: Zero Sum的更多相关文章
- 【USACO 2.3】Zero Sum(dfs)
按字典序输出所有在123..n之间插入'+','-',' '结果为0的表达式.. http://train.usaco.org/usacoprob2?a=jUh88pMwCSQ&S=zeros ...
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
- USACO section 1.1 C++题解
USACO section1.1:DONE 2017.03.03 TEXT Submitting Solutions DONE 2017.03.04 PROB Your Ride Is Here [A ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- USACO Section 3.3 游戏 A Game
OJ:http://www.luogu.org/problem/show?pid=2734 #include<iostream> #include<cstring> using ...
- USACO Section 3.3: A Game
第一次碰到博弈论题目,是很棘手,博弈论题目要考虑全局最优的解法,我第一次用了局部最优的,而且vector也没pop_front()操作.后来看了网上的用dp的方法解的. 博弈论的题目基本都得用dp法子 ...
- USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
- USACO Section 2.2: Subset Sums
dp题,一碰到dp我基本就是跪,搜了网上的答案分两种,一维和二维. 先讲二维,sum[i][j]表示前i个数的subset里差值为j的分法数量.当加入数字i时,有两种选择,某一个set和另外一个set ...
- USACO Section 2.1 Sorting a Three-Valued Sequence
/* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...
随机推荐
- 这个好像、也许、或许、大概、应该、Maybe真的可以算是传说中的Spring.Net了吧
这个好像.也许.或许.大概.应该.Maybe真的可以算是传说中的Spring.Net了吧 ...
- Netsharp快速入门(之13) 销售管理(单据流转 销售订单生成发货单)
作者:秋时 杨昶 转载须说明出处 4.5 单据流转 4.5.1 单据流转的目的 单据流转主要为了实现业务关系的流转,并记录相互之间的关系.例如从销售订单生成销货单,两张单据之间有对应的关 ...
- OS X 使用技巧——在Finder窗口标题栏上显示路径
Finder窗口默认显示当前文件夹的名称或当前所在的模式(例如AirDrop).如果想要显示路径(用User/[当前用户账号名称]/Documents 替代以前显示的Documents),打开终端并运 ...
- Window.document对象(1)
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- C语音的调查
1.你对自己的未来有什么规划?做了哪些准备? 很迷茫吧.尽力做好该做的事情.希望认真学习代码,毕竟是专业课 2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 学习有很多啊,不论是课堂上, ...
- Memcached使用
一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...
- navigationController.navigationBar.titleTextAttributes
navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor ...
- livevent的几个问题
关于libevent的几个问题 1.他到底是如何保证这个套接字有效的 主线程去断开关闭套接字,主线程去调用buffwrite,在子线程收到客户端断开时,主线程延迟1分钟释放,这样保证子线程操作完该so ...
- The Brain as a Universal Learning Machine
The Brain as a Universal Learning Machine This article presents an emerging architectural hypothesis ...
- 在线最优化求解(Online Optimization)之五:FTRL
在线最优化求解(Online Optimization)之五:FTRL 在上一篇博文中中我们从原理上定性比较了L1-FOBOS和L1-RDA在稀疏性上的表现.有实验证明,L1-FOBOS这一类基于梯度 ...