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. python学习笔记(5)—— tuple 本质探究

    >>> t=(1,2,3,['a','b','c'],4,5) >>> t[3][0]='x' >>> t (1, 2, 3, ['x', 'b' ...

  2. facade模式-服务-配置----系统生成与配置

    facade模式对外提供一组相关服务: 对内整合子系统: facade模式的创建过程需要依赖外部的配置. 配置完成以后才能使用服务. 推广开来,所有系统都需要生成与配置,然后才能对外提供服务.

  3. Java 8 和 Java 9部分区别

    Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总.Java 8 中 Concurrent package的改变java.util.concurrent中 ...

  4. JavaScipt30(第五个案例)(主要知识点:flex布局)

    承接上文,这是第5个案例:这节没什么讲的,随便记录下吧,主要是用了flex布局与transform translateY,js部分和案例1类似. 附上项目链接: https://github.com/ ...

  5. <东方梦符祭> N2无尽40波通关

    先上图吧 阵容:纯粹 + 伪魔法队 主C:神妈 露米娅(我觉得不厉害了) 灵梦 控制:琪露诺 + 蕾蒂 永江依玖(听说很厉害 没培育满 没看到效果) 挂件:铃仙挂机 帕秋莉 大妖精(链神妈) 圣今天才 ...

  6. C# Task详解

    1.Task的优势 ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便.比如: ◆ ThreadPool不支持线程的取消.完成.失败通知等交互性 ...

  7. PrintWriter与ServletOutputStream的区别之文件下载

    copy自:https://blog.csdn.net/weixin_37703598/article/details/803870611.out = response.getWriter(); re ...

  8. python json、 pickle 、shelve 模块

    json 模块 用于序列化的模块 json,用于字符串 和 python数据类型间进行转换 Json模块提供了四个功能:dumps.dump.loads.load #!/usr/bin/env pyt ...

  9. Jmeter BeanShell PreProcessor使用笔记

    打印log log.info("content:" + content); 将字符串转化为JsonString import com.alibaba.fastjson.JSON; ...

  10. 关于zookeeper和zkfc的一些测试

    1.停掉zookeeper集群 ****进程影响****** zkfc:报错无法连接zookeeper.ClientCnxn java.net.connectexception:拒绝连接,但不会shu ...