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

Here are the test data inputs:

  1. ------- test 1 ----
  2. 3
  3. ------- test 2 ----
  4. 4
  5. ------- test 3 ----
  6. 5
  7. ------- test 4 ----
  8. 6
  9. ------- test 5 ----
  10. 7
  11. ------- test 6 ----
  12. 8
  13. ------- test 7 ----
  14. 9

Keep up the good work!

Thanks for your submission!

it's a easy problem of dfs

  1. /*
  2. ID:kevin_s1
  3. PROG:zerosum
  4. LANG:C++
  5. */
  6.  
  7. #include <iostream>
  8. #include <cstdio>
  9. #include <string>
  10. #include <cstring>
  11. #include <vector>
  12. #include <map>
  13. #include <set>
  14. #include <algorithm>
  15. #include <cstdlib>
  16. #include <list>
  17. #include <cmath>
  18.  
  19. using namespace std;
  20.  
  21. //gobal variable====
  22. int N;
  23. vector<string> result;
  24. //==================
  25.  
  26. //function==========
  27. char NumToChar(int i){
  28. char ch = i + 48;
  29. return ch;
  30. }
  31.  
  32. void DFS(int i, int sum, string str, int last_operator){
  33. if(i > N + 1)
  34. return;
  35. if(i == N + 1){
  36. if(sum == 0){
  37. result.push_back(str);
  38. }
  39. return;
  40. }
  41. //plus
  42. string tmp1 = str;
  43. tmp1 = tmp1 + "+" + NumToChar(i);
  44. DFS(i + 1, sum + i, tmp1, i);
  45. //minus
  46. string tmp2 = str;
  47. tmp2 = tmp2 + "-" + NumToChar(i);
  48. DFS(i + 1, sum - i, tmp2, -i);
  49. //multiply
  50. string tmp3 = str;
  51. tmp3 = tmp3 + " " + NumToChar(i);
  52. int cc = 0;
  53. if(last_operator > 0)
  54. cc = 1;
  55. else
  56. cc = -1;
  57. int mt = cc * (abs(last_operator) * 10 + i);
  58. int sum_tmp = sum - last_operator + mt;
  59. DFS(i + 1, sum_tmp, tmp3, mt);
  60. return;
  61. }
  62.  
  63. //==================
  64.  
  65. int main(){
  66. freopen("zerosum.in","r",stdin);
  67. freopen("zerosum.out","w",stdout);
  68. cin>>N;
  69. string str = "1";
  70. DFS(2, 1, str, 1);
  71. sort(result.begin(), result.end(), less<string>());
  72. vector<string>::iterator iter;
  73. for(iter = result.begin(); iter != result.end(); iter++){
  74. cout<<*iter<<endl;
  75. }
  76. return 0;
  77. }

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. vue中的input使用e.target.value赋值的问题

    很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...

  2. PHPExcel导入

    PHPExcel 是用来操作Office Excel 文档的一个PHP类库,可以使用它来读取.写入不同格式的电子表格 Github:https://github.com/PHPOffice/PHPEx ...

  3. ThinkPHP---thinkphp会话支持和文件载入

    [一]会话控制 会话支持一般指cookie和session,在ThinkPHP里为了方便开发,封装了cookie和session方法. (1)session方法 在函数库封装了session方法 se ...

  4. 微服务网关从零搭建——(三)Ocelot网关 + identity4

    增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...

  5. vue的路由配置

    路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...

  6. 动态生成java、动态编译、动态加载

    我曾经见过一个“规则引擎”,是在应用系统web界面直接编写java代码,然后保存后,规则即生效,我一直很是奇怪,这是如何实现的呢?实际这就好像jsp,被中间件动态的编译成java文件,有被动态的编译成 ...

  7. Object.prototype 原型和原型链

    Object.prototype 原型和原型链 原型 Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,有些属性是隐藏的.换句话说,在对象创建 ...

  8. typora_test

    加粗标题 加下标线 <!--aba--> #Include ![](C:\Users\123\Pictures\Saved Pictures\1.jpg) ![](http://gyz.g ...

  9. 挂载本地file到容器中

    -v /Us……/cts/fffen:/usr/local/src -v 标记 将本地主机的目录 到 目标容器的路径下 在容器中查看:ls  发现已经存在py文件 运行python fenci.py ...

  10. Python学习——集合

    集合 python中的集合和数学上集合具有基本相同的性质,此处不再赘述. 1.创建集合的两种方法 #直接创建 num={1,2,3,4,5} #利用set方法创建 num1=set([1,2,3,4, ...