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

 /*
 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中求年龄

    时间函数: 1.curdate() --- 当前系统日期 调取: select curdate() 2.curtime() --- 当前系统时间 调取: select curtime() 3.now( ...

  2. bzoj 3232 01分数规划+最大权封闭子图判定

    我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...

  3. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...

  4. Centos编译安装PHP 5.5笔记

    本篇是在 Centos 6.4 32bit 下编译安装 php 5.5.5 的笔记,接上篇 Centos编译安装Apache 2.4.6笔记.php 5.5.x 和 centos 源里面的 php 5 ...

  5. windows_phone指定时间后执行函数

    开发windows phone 应用程序时需要在一段指定的时间后执行某些函数,于是乎想到了通过DispatcherTimer类来实现,再在.Tick后面添加自己想要的事件 DispatcherTime ...

  6. ubuntu学习之一

    1终端 在右键后菜单中找到 终端,(如果没有终端,使用 Ctrl+Alt+[F1~F6] ,您可以切换到1~6号控制台,输入: sudo apt-get install nautilus-open-t ...

  7. C# 序列化 Serialize 的应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. Docker初识

    <Docker--从入门到实践>是Docker技术的入门教程,学习时长两天,现整理关键点如下: 1. 什么是Docker? 轻量级操作系统虚拟化解决方案:Go语言实现:下图很好地说明了Do ...

  9. leetcode majority number

    给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...

  10. ZOJ2929 Penalty Kick(概率)

    题目挺水的,但由于其独特的阅读量比赛的时候没发现这道水题,在此做一下翻译,如果有人搜到这翻译的话有帮助的话自然最好啦. 中国队平局进入最后的点球决胜局,首先抛硬币决定谁先罚球,然后先是罚五球,如果罚的 ...