USER: Kevin Samuel [kevin_s1]
TASK: zerosum
LANG: C++ Compiling...
Compile: OK Executing...
Test 1: TEST OK [0.003 secs, 3508 KB]
Test 2: TEST OK [0.003 secs, 3508 KB]
Test 3: TEST OK [0.005 secs, 3508 KB]
Test 4: TEST OK [0.000 secs, 3508 KB]
Test 5: TEST OK [0.005 secs, 3508 KB]
Test 6: TEST OK [0.008 secs, 3508 KB]
Test 7: TEST OK [0.014 secs, 3508 KB] All tests OK. YOUR PROGRAM ('zerosum') WORKED FIRST TIME! That's fantastic
-- and a rare thing. Please accept these special automated
congratulations.

Here are the test data inputs:

------- test 1 ----
3
------- test 2 ----
4
------- test 3 ----
5
------- test 4 ----
6
------- test 5 ----
7
------- test 6 ----
8
------- test 7 ----
9

Keep up the good work!

Thanks for your submission!

it's a easy problem of dfs

/*
ID:kevin_s1
PROG:zerosum
LANG:C++
*/ #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath> using namespace std; //gobal variable====
int N;
vector<string> result;
//================== //function==========
char NumToChar(int i){
char ch = i + 48;
return ch;
} void DFS(int i, int sum, string str, int last_operator){
if(i > N + 1)
return;
if(i == N + 1){
if(sum == 0){
result.push_back(str);
}
return;
}
//plus
string tmp1 = str;
tmp1 = tmp1 + "+" + NumToChar(i);
DFS(i + 1, sum + i, tmp1, i);
//minus
string tmp2 = str;
tmp2 = tmp2 + "-" + NumToChar(i);
DFS(i + 1, sum - i, tmp2, -i);
//multiply
string tmp3 = str;
tmp3 = tmp3 + " " + NumToChar(i);
int cc = 0;
if(last_operator > 0)
cc = 1;
else
cc = -1;
int mt = cc * (abs(last_operator) * 10 + i);
int sum_tmp = sum - last_operator + mt;
DFS(i + 1, sum_tmp, tmp3, mt);
return;
} //================== int main(){
freopen("zerosum.in","r",stdin);
freopen("zerosum.out","w",stdout);
cin>>N;
string str = "1";
DFS(2, 1, str, 1);
sort(result.begin(), result.end(), less<string>());
vector<string>::iterator iter;
for(iter = result.begin(); iter != result.end(); iter++){
cout<<*iter<<endl;
}
return 0;
}

USACO zerosum DFS 1A的更多相关文章

  1. hdu 4277 USACO ORZ dfs+hash

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  2. hdu 4277 USACO ORZ DFS

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU4277 USACO ORZ(dfs+set)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  4. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  5. 【USACO 2.3】Zero Sum(dfs)

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

  6. Usaco 2.3 Zero Sums(回溯DFS)--暴搜

    Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...

  7. USACO Hamming Codes DFS 构造

    我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...

  8. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  9. 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)

    http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...

随机推荐

  1. iOS从手机相册选择一张照片并显示 Objective-C

    要先给app设置访问相册的权限: 在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限 ViewController.h: #i ...

  2. 【Linux】centos7 添加脚本到/etc/rc.local文件里,实现开机自启

    Linux 设置开机自启动,添加命令到/etc/rc.d/rc.local,本文以设置tomcat自启动为例: 一:添加自启动命令 export JAVA_HOME=/usr/java/jdk1.8. ...

  3. vue项目 build之后发布到服务器index.html页面空白解决方法

    第一部分 之前一直不太理解为什么要使用vue+webapck,还有在使用了vue-cli之后会用到后台,即vue-cli自动帮我们安装了express服务器,在本地服务器上运行,因为我们希望可以模拟在 ...

  4. HTML5 实现Link跳线效果

    之前我们推出过Flex版本的Link跳线效果,现在基于HTML5新版本的跳线效果也实现了,细微之处我们进行了改进,如link倾斜的时候Offset方向始终保持垂直等.先看效果.实现的算法和Flex基本 ...

  5. <MySQL>入门三 数据定义语言 DDL

    -- DDL 数据定义语言 /* 库和表的管理 一.库的管理:创建.修改.删除 二.表的管理:创建.修改.删除 创建:create 修改:alter 删除:drop */ 1.库的管理 -- 库的管理 ...

  6. springcloud(十二):Ribbon客户端负载均衡介绍

    springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...

  7. 【DIP, 图像增强】

    第四章 图像增强 图像增强是按特定的需要突出一幅图像中的某些信息,同时削弱或者去除某些不需要的信息的处理方法.其主要目的是使处理后的图像对某种特定的应用来说,比原始图像更加适用.因此这类处理是为了某种 ...

  8. nyoj 63 小猴子下落

    小猴子下落 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从左到右从上到下的编号为1,2,3,····· ...

  9. [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  10. 网线切割&切绳子

    题目描述 Wonderland居民决定举行一届地区性程序设计大赛.仲裁委员会志愿负责这次赛事并且保证会组织一次有史以来最公正的比赛.为此,所有参赛者的电脑和网络中心会以星状网络连接,也就是说,对每个参 ...