USACO zerosum DFS 1A
- 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的更多相关文章
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU4277 USACO ORZ(dfs+set)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- 2012年长春网络赛(hdu命题)
为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...
- 【USACO 2.3】Zero Sum(dfs)
按字典序输出所有在123..n之间插入'+','-',' '结果为0的表达式.. http://train.usaco.org/usacoprob2?a=jUh88pMwCSQ&S=zeros ...
- 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 ... ...
- USACO Hamming Codes DFS 构造
我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)
http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...
随机推荐
- vue中的input使用e.target.value赋值的问题
很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...
- PHPExcel导入
PHPExcel 是用来操作Office Excel 文档的一个PHP类库,可以使用它来读取.写入不同格式的电子表格 Github:https://github.com/PHPOffice/PHPEx ...
- ThinkPHP---thinkphp会话支持和文件载入
[一]会话控制 会话支持一般指cookie和session,在ThinkPHP里为了方便开发,封装了cookie和session方法. (1)session方法 在函数库封装了session方法 se ...
- 微服务网关从零搭建——(三)Ocelot网关 + identity4
增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...
- vue的路由配置
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...
- 动态生成java、动态编译、动态加载
我曾经见过一个“规则引擎”,是在应用系统web界面直接编写java代码,然后保存后,规则即生效,我一直很是奇怪,这是如何实现的呢?实际这就好像jsp,被中间件动态的编译成java文件,有被动态的编译成 ...
- Object.prototype 原型和原型链
Object.prototype 原型和原型链 原型 Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,有些属性是隐藏的.换句话说,在对象创建 ...
- typora_test
加粗标题 加下标线 <!--aba--> #Include  ![](http://gyz.g ...
- 挂载本地file到容器中
-v /Us……/cts/fffen:/usr/local/src -v 标记 将本地主机的目录 到 目标容器的路径下 在容器中查看:ls 发现已经存在py文件 运行python fenci.py ...
- Python学习——集合
集合 python中的集合和数学上集合具有基本相同的性质,此处不再赘述. 1.创建集合的两种方法 #直接创建 num={1,2,3,4,5} #利用set方法创建 num1=set([1,2,3,4, ...