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;
}

最新文章

  1. ExtJS笔记 Ext.data.Types
  2. js倒计时跳转链接
  3. Scala.js v0.1 发布,在浏览器直接运行 Scala
  4. HDU 4433 locker(SPFA+DP)
  5. &lt; java.util &gt;-- Iterator接口
  6. BZOJ 3969 Low Power 解题报告
  7. java中dao层的通用层,通过反射机制,操作数据库的增删改,适用的范围是不包含属性类
  8. 《Linux多线程服务端编程》笔记——多线程服务器的适用场合
  9. PHP中的for循环
  10. 201521123103 《java学习笔记》 第十周学习总结
  11. 深度解剖sesssion运行原理
  12. 拆轮子之Fish动画分析
  13. java编写词法分析器
  14. 爬虫之urllib
  15. 十一、eclipse如何创建一个maven工程project
  16. unigui+fastReport实现web打印方案
  17. Nordic Collegiate Programming Contest NCPC 2017-Problem G Galactic Collegiate Programming Contest
  18. JavaScript 函数声明与函数表达式的区别 函数声明提升(function declaration hoisting)
  19. rnnlib依赖ubuntu环境配置
  20. 作为sort()方法的参数的比较函数(高程三第五章)

热门文章

  1. HiveServer2后台运行
  2. 安卓app测试之cpu监控
  3. docker 转载
  4. php第二十八节课
  5. C++ string 是否以‘\0’结尾 讨论
  6. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)
  7. docker 部署spring.boot项目【一】(引用外部配置文件)
  8. Linux 服务器 U盘安装(避免U盘启动)以及拔除U盘后无法引导系统
  9. ROW_NUM
  10. RAII手法封装互斥锁