这题我做得比较麻烦,网上有个比较简单的程序。

 /*
 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的更多相关文章

  1. 【USACO 2.3】Zero Sum(dfs)

    按字典序输出所有在123..n之间插入'+','-',' '结果为0的表达式.. http://train.usaco.org/usacoprob2?a=jUh88pMwCSQ&S=zeros ...

  2. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

  3. 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 ...

  4. 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&& ...

  5. USACO Section 3.3 游戏 A Game

    OJ:http://www.luogu.org/problem/show?pid=2734 #include<iostream> #include<cstring> using ...

  6. USACO Section 3.3: A Game

    第一次碰到博弈论题目,是很棘手,博弈论题目要考虑全局最优的解法,我第一次用了局部最优的,而且vector也没pop_front()操作.后来看了网上的用dp的方法解的. 博弈论的题目基本都得用dp法子 ...

  7. USACO Section 3.3: Riding the Fences

    典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...

  8. USACO Section 2.2: Subset Sums

    dp题,一碰到dp我基本就是跪,搜了网上的答案分两种,一维和二维. 先讲二维,sum[i][j]表示前i个数的subset里差值为j的分法数量.当加入数字i时,有两种选择,某一个set和另外一个set ...

  9. USACO Section 2.1 Sorting a Three-Valued Sequence

    /* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...

随机推荐

  1. android开发 wifi开发不稳定性测试

    场景:工厂定制机器,要求一个设备创建wifi热点,一个设备去连接.但是现在发现wifi连接很不稳定,主要以下3方面: 1.连接之前,不容易连接上 2.连接上之后,连不到外网 3.连接上之后,稳定性不好 ...

  2. python--str的几个方法

    str.format()  :对应取值 name="chenshan" age=30 address="宜山路926号新思大厦15楼" print " ...

  3. ZOJ 3941 Kpop Music Party 贪心

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3941 题解: 先吧所给的区间合并,得到若干个独立的区间. 然后从左 ...

  4. 【BZOJ】【1503】 【NOI2004】郁闷的出纳员

    Splay Splay的模板题吧……妥妥的序列操作= =(好像有段时间没写过这种纯数据结构题了……) /************************************************ ...

  5. 浅谈KL散度

    一.第一种理解 相对熵(relative entropy)又称为KL散度(Kullback–Leibler divergence,简称KLD),信息散度(information divergence) ...

  6. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  7. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  8. Reactor构架模式

    http://www.cnblogs.com/hzbook/archive/2012/07/19/2599698.html Reactor框架是ACE各个框架中最基础的一个框架,其他框架都或多或少地用 ...

  9. javascript设计模式--单例模式(Singleton)

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  10. Sqli-labs less 15

    Less-15 本关没有错误提示,那么我们只能靠猜测进行注入.这里我直接从源代码中看到了sql语句 @$sql="SELECT username, password FROM users W ...