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 ...
随机推荐
- windows python 打印utf-8乱码
从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors
题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...
- 【BZOJ】【2324】【ZJOI2011】拯救皮卡丘
网络流/费用流+Floyed Orz zyf 题解: 这题和星际竞速还有打印机两题的主体思路都是一样的 每个点一定要经过,并且要经过这个点,必须经过比这个点小的所有点.而且还存在一个附加源,但源到附加 ...
- 【转】Sublime text 3 中文文件名显示方框怎么解决
引用自:http://www.zhihu.com/question/24029280 如图,中文文件名打开全是乱码,内容倒是装了converttoutf8没什么太大的问题. 这个是sublime te ...
- 解决iptables和vsftpd设置的问题
解决iptables和vsftpd设置的问题 博客分类: linux/centos/ubuntu 防火墙J#工作 解决iptables和vsftpd设置的问题 修改 vi /etc/sysconfig ...
- SELINUX设为Disable 影响java SSH工具包Jsch 0.1.49.jar的一个案例
最近项目中遇到一个典型事件,当RHEL 的SELINUX设为DISABLE时 使用JAVA的Jsch 库调用SSH命令时将随机返回空字符串,我使用的版本是0.1.49,最新版本0.1.51未测试. 关 ...
- 算术编码Arithmetic Coding-高质量代码实现详解
关于算术编码的具体讲解我不多细说,本文按照下述三个部分构成. 两个例子分别说明怎么用算数编码进行编码以及解码(来源:ARITHMETIC CODING FOR DATA COIUPRESSION): ...
- Bit-Map
昨日读July大神<教你如何迅速秒杀掉:99%的海量数据处理面试题>博客,有这么一题与大家分享: 给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断 ...
- daatable动态创建
方法一: DataTable tblDatas = new DataTable("Datas");DataColumn dc = null;dc = tblDatas.Column ...
- cocos2d-x3.0环境搭建(基于win7以及mac)
流程概览: Windows平台 一.安装 Python与配置Python环境变量 二.安装Cocos2d-x,并创建项目 Mac平台 安装Cocos2d-x,并创建项目 具体操作: 一.安装Pytho ...