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 ...
随机推荐
- Linux服务器文件权限被改
阿里云买的ubuntu服务器遭受了不明攻击,导致站点访问不了,折腾了很久,才发现是文件的权限被修改了.然后就是一点点的修改,很是麻烦.服务器的安全要重视呢! 1.修改权限 chmod 755 * -R ...
- Win7访问不了WINXP共享文件
用win xp的机器可以访问,但用win 7的机器无法访问共享文件 提示:您没有权限访问.请与网络管理员联系请求访问权限 网上找了相应的资料 做了如下动作 1. 打开网上邻居→本地连接→属性里,“看是 ...
- SpringBoot项目的mybatis逆向工程
<dependencies> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot& ...
- Xamarin.Forms android实现沉浸式
在android项目里,这样设置 using System; using Android.App; using Android.Content.PM; using Android.Runtime; u ...
- 导出功能在数据库内容为数字,excel表格中是汉字的时候
代码如下: @ExcelField(title = "饮水器评价",dictType = "waterer_rate" ,align = 2, sort = 2 ...
- list查询棚舍面积的时候,所有棚舍面积的value都是一样的
解决办法 将pickingid在查list之前set到对象中,通过id来匹配查询 具体代码: FarmHouse farmHouse=new FarmHouse(); farmHouse.setPic ...
- org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: xxxxxxx 原因是 ...
- datatable 分组
public static void PrintPersons() { //准备数据 DataTable dt = new DataTable(); dt.Columns.Add(new DataCo ...
- 百练4103:踩方格(DFS)
描述 有一个方格矩阵,矩阵边界在无穷远处.我们做如下假设:a. 每走一步时,只能从当前方格移动一格,走到某个相邻的方格上:b. 走过的格子立即塌陷无法再走第二次:c. 只能向北.东. ...
- Java中list集合ArrayList 中contains包含的使用
Java中list集合ArrayList 中contains包含的使用 https://blog.csdn.net/qq_38556611/article/details/78774690