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

 /*
 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. 阿里云服务器mysql修改编码问题

    最近在学习struts+spring+hibernate,强烈推荐新手一本书:陈天河<轻量级web应用开发>,这本书是我见过的国内最好的书,初学者可以买本读读. 不说这个了,来说说我的问题 ...

  2. Xamarin.Android之转换,呼叫,查看历史纪录

    Xamarin.Android之转换,呼叫,查看历史纪录 E文文章. 功能:能将输入的字母转换成相应的数字.并且能呼叫出去.能查看呼叫的历史纪录. 界面代码如下: <?xml version=& ...

  3. Centos6.5以下Samba服务配置

    一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...

  4. cookie和session的代码实现

    cookie和session的代码实现 1.设置cookie 今天笔试题考的是cookie的设置,我竟然选了request也可以设置cookie,我的天呀. 我们来看如何在response设置吧 pu ...

  5. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  6. 【bzoj1005】[HNOI2008]明明的烦恼

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4175  Solved: 1660[Submit][Stat ...

  7. fpu栈溢出

    老大们遇到个问题,有一堆浮点数运算,分开写就对,合一起就溢出. 是因为定义的函数返回float的时候,别的地方声明是void错了,这样的错误累计八次之后,浮点数寄存器就满了.没地方放就错了. 函数前面 ...

  8. LCA 笔记

    简述这几天的LCA 心得: LCA有两种做法:离线 or 在线 先学的离线: 原理博客很多. 我写得两道题,已经模板. HDU:http://acm.hdu.edu.cn/showproblem.ph ...

  9. HDU 4493 Tutor(精度处理)

    题目 #include<stdio.h> int main() { int t; double a,s; scanf("%d",&t); while(t--) ...

  10. 相对布局RelativeLayout

      一. public class RelativeLayout extends ViewGroup java.lang.Object    ↳ android.view.View      ↳ an ...