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 ...
随机推荐
- Oracle Client Language Problem
If you execute SP in the Oracle client and got the error like this: oracle.xdo.XDOException: oracl ...
- poj 2342 Anniversary party
题目链接:http://poj.org/problem?id=2342 题意:读题很容易懂,这里不做介绍. 解法:树形DP之路的第一道题. #include<iostream> #incl ...
- 【CodeForces】【338E】Optimize!
线段树 先搞出来每个a[i]能连多少条边记为w[i]……如果对一组s[i],都满足w[i]-rank[i]>=0则这是一组合法方案,然后线段树维护w[i]-rank[i](第一个元素出去的时候后 ...
- poi实现Excel比较
http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java http://stac ...
- 火狐和IE之间的7个JavaScript差异
尽管 JavaScript 历史上使用冗长而令人生厌的代码块来标的特定浏览器的时期已经结束了,但是偶尔使用一些简单的代码块和对象检测来确保一些代码在用户机器上正常工作依然是必要的. 这篇文章中,我会略 ...
- logback日志配置文件代码示例
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true&q ...
- ISIN编码
国际证券识别编码(ISIN编码)是由国际标准化组织(ISO)制定的证券编码标准,并在<证券及相关金融工具-国际证券识别编码体系>(ISO6166)中正式发布.ISO6166主要规定了ISI ...
- jboss 占用cpu 100%
通过Java thread dump分析找到耗费CPU最高的源代码 分类: 9. Java2010-04-11 23:06 9272人阅读 评论(4) 收藏 举报 threadjavaeclipse插 ...
- C#修改注册表设置默认浏览器
项目中用到VPN技术登录来访问内部网络的应用系统,VPN客户端连接后会自动以默认浏览器来打开站点,由于应用系统使用的前端框架对IE浏览器版本要求较高,而用户大多数的电脑里安装的IE的版本都较低,于是想 ...
- 用Stopwatch类获得程序运行时间
我们可以用Stopwatch类获得程序的运行时间,在优化代码时,可以用此方法来查看优化前后程序所耗费的时间 //Stopwatch类別在System.Diagnostics命名空间里 Stopwatc ...