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 ...
随机推荐
- android 开发怎么让程序生成的图片文件不会被系统扫描到
我们在写应用的时候,可能会保存很多图片,大的小的,仅仅是我们的应用中会用到,处于种种原因不希望用户看到,我是觉着如果被用户看到了,就失去了我的应用的那一层神秘的面纱,用户是米有闲情逸致去打开你一层层的 ...
- SD 销售订单的BADI增强SD_BADI_PRICE_CHECK
METHOD if_ex_badi_sd_sales~save_document.* CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'* EXPORTING* titel = ...
- TCP 粘包/拆包问题
简介 TCP 是一个’流’协议,所谓流,就是没有界限的一串数据. 大家可以想想河里的流水,是连成一片的.期间并没有分界线, TCP 底层并不了解上层业务数据的具体含义 ,它会根据 TCP 缓冲区 ...
- SpringMVC:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;
今天用SpringMVC做修改添加操作,之前的操作都实现了添加修改,但始终报com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have ...
- 监听HTML input输入框值的即时变化onpropertychange、oninput兼容IE,Chrome,FF,Opera等
转自:http://blog.csdn.net/itchiang/article/details/7769337 要达到的效果 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增 ...
- STMPClient 发送邮件显示 不允许使用邮件名称.
在.net 2.0,3.5, 针对某些邮箱(还不清楚是什么样的邮件) , 使用微软自带的DLL发送邮件会提示不允许使用邮件名称 .... 使用Jmail可以发送. 解决方案: 1. ...
- CSS Hack大全-可区分出IE6-IE10、FireFox、Chrome、Opera
今天把一些常用的CSS Hack整理了一下,包括常用的IE hack以及火狐.Chrome.Opera浏览器的Hack,并把这些CSS Hack综合的一起,写了一个小的浏览器测试器.如图所示: 下面就 ...
- SQL分页查询总结{转}
开发过程中经常遇到分页的需求,今天在此总结一下吧.简单说来方法有两种,一种在源上控制,一种在端上控制.源上控制把分页逻辑放在SQL层:端上控制一次性获取所有数据,把分页逻辑放在UI上(如GridVie ...
- Android Environment FAQ (Frequently Asked Question)
1.how to find out the Eclipse Version From Eclipse Menu Help ----> About Eclipse It displayed as ...
- this的使用、继承、super
1.this的使用 1)可以用于区分局部变量 Person(int age,string name) { this.age=age; this.name=name; } 2)构造方法中,用this调用 ...