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 ...
随机推荐
- Eclipse的python插件安装
网上找了一些资料都没有成功~~然后自己装的过程中编辑记录了一些 当然博客园里也有人用这一种方法也可以参考IBM中的 http://www.cnblogs.com/visec479/p/4139882. ...
- Domain many-to-many
class TakeAwardAct { String title; static hasMany=[awards:Award] static constraints = { } String toS ...
- 使用WatchService监控指定目录内的文件的改动
package coin; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Pat ...
- JS设计模式——5.单体模式
JS设计模式——5.单体模式 http://www.cnblogs.com/JChen666/p/3610585.html 单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? ...
- 查看Centos系统信息命令
linux命令行具有强大的功能,我们安装vps后,首先应该知道系统信息,查看这些信息,你会发现Linux命令很简单,你可以按照下面的命令练习. linux系统信息 # uname -a # 查看内核/ ...
- Feature Engineering versus Feature Extraction: Game On!
Feature Engineering versus Feature Extraction: Game On! "Feature engineering" is a fancy t ...
- console中一些不常用的实用方法
console.group('分组1'); console.table( [ {key1: 1,key2: 2,key3: 3}, {key1: 1,key2: 2,key3: 3}, {key1: ...
- D3D11 Debug Layer的bug
在开发D3D应用程序时,我们会使用Debug Layer来调试应用程序,以确保我们的程序在最终发布时没有warnings和errors.不过最近在开发应用程序时遇到了这样的问题,就是我把多个网格模型的 ...
- 浏览器执行js
Scriptish chrome自带 greasemonkey http://www.firefox.net.cn/forum/viewtopic.php?f=5&t=45715
- 使用 polyfills 的简易方法
本文作者为 Andrew Betts 与 Robert Nyman.Andrew 是金融时报(Financial Times)实验室主任,该实验室旨在金融时报开发并推广实践性的 Web 技术.Robe ...