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

 /*
 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. 网站中的专题页或者tag聚合页的权重不错

    一.据最近的一些观察,觉得网站中的专题页或者tag聚合页的权重不错,因此多给网站制作一些专题页面,不仅有利于聚合站内的文章,更是绝对的原创内容,应该会受到百度的青睐.简评:关于权重的讨论,这篇无疑是很 ...

  2. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  3. 【CentOS】安装配置vncserver

    参考资料: http://my.oschina.net/yankunren/blog/70042 安装过程 (1).检查vncserver是否已经安装 [root@Nginx canyouNgx]# ...

  4. bzoj 2751 快速幂

    首先我们知道,对于所有种情况,我们可以将每一位可以放的 数的值加起来,所有位置的乘起来,等于的就是最后的答案,具体 为什么正确,可以根据乘法分配律来想一想. 那么对于所有不做要求的,快速幂直接算就行了 ...

  5. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  6. 在eclipse中配置maven

    http://pansanday.blog.163.com/blog/static/381662802012727103454743/ 从eclipse 3.6开始,eclipse有一个marketp ...

  7. hdu 1171

    求能装入大小为sum/2的背包的最大价值 #include <cstdio> #include <cstdlib> #include <cmath> #includ ...

  8. IOS Crash捕获

    IOS Crash ,就两种情况:一种是异常,另一种是中断[信号量]. #include <libkern/OSAtomic.h> #include <execinfo.h> ...

  9. Apache POI 解析 microsoft word 图片文字都不放过

    http://blog.csdn.net/njchenyi/article/details/6894500 http://haohaoxuexi.iteye.com/blog/2031335

  10. Struts2 Convention插件的使用(2)return视图以及jsp的关系

    package com.hyy.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extend ...