USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
给你N。
把1到N依次写出,每相邻两个数字中间加上一个字符,三选一:'+','-',' '。
如此,便可形成很多表达式,把其中计算结果为0的按字典序输出。
【数据范围】
3<=N<=9
【输入样例】
7
【输出样例】
1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
枚举。脑子清楚,代码别写错就好。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
一遍AC。
------------------------------------------------------------------------------------------------------------------------------------------------
【代码】
/*
ID: icedrea1
PROB: zerosum
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; int N;
char mark[]; int get(int &l)
{
int r=l;
while(r<N && mark[r]==' ') ++r;
int x=;
for(int i=l;i<=r;++i) x=x*+i;
l=r+;
return x;
} void get(int i,char &c)
{
c=mark[i-];
} void print(ostream& out)
{
for(int i=;i<=N-;++i) out<<i<<mark[i];
out<<N<<endl;
} void test(ostream& out)
{
//cout<<"test: "; print(cout);
int r=;
char c='+';
for(int i=,num=get(i);;num=get(i))
{
if(c=='+') r+=num; else r-=num;
if(i==N+) break; else get(i,c);
}
if(r==) print(out);
} void go(int i,ostream& out)
{
//cout<<i<<endl;
if(i==N) { test(out); return; }
mark[i]=' '; go(i+,out);
mark[i]='+'; go(i+,out);
mark[i]='-'; go(i+,out);
} int main()
{
//printf("%d %d %d\n",'+','-',' '); ifstream in("zerosum.in");
ofstream out("zerosum.out"); in>>N; go(,out); in.close();
out.close();
return ;
}
USACO Section2.3 Zero Sum 解题报告 【icedream61】的更多相关文章
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
holstein解题报告 --------------------------------------------------------------------------------------- ...
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.2 Preface Numbering 解题报告 【icedream61】
preface解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.3 Controlling Companies 解题报告 【icedream61】
concom解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Money Systems 解题报告 【icedream61】
money解题报告------------------------------------------------------------------------------------------- ...
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Longest Prefix 解题报告 【icedream61】
prefix解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- Open XML的上传、下载 、删除 ......文件路径
/// <summary> /// Get download site, if download tempfolder not existed, create it first /// & ...
- vue中列表的过渡
<style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-active,.v-leave-active{ transition: opaci ...
- set方法的使用
<div id='root'> <div v-for='(item,key,index) of userInfo'> {{item}}--{{key}}--{{index}} ...
- P1424 小鱼的航程(改进版)
题目背景 原来的题目太简单,现改进让小鱼周末也休息,请已经做过重做该题. 题目描述 有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),假设从周x(1<=x&l ...
- redis 系列 在 vs上 set,get 键值
1.启动两个 cmd,一个用于打开服务,一个用于运行客户端. 详细步骤可见上一篇文章 2.下载nuget的 ServiceStack.Redis; ,并在using中引用 ,详细步骤可见上一篇文章 ...
- 【转】git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚
转载自:http://m.blog.csdn.net/blog/lihongli528628/45483463 [git 删除本地分支] git branch -D br [git 删除远程分支] g ...
- matlab所需插件
1
- CSS font-size字体大小样式属性
设置字体大小CSS单词与语法 基本语法结构: .divcss5{font-size:12px;}设置了文字大小为12px像素Font-size+字体大小数值+单位 单词:font-size语法:fon ...
- e.preventdefault() 别滥用
有的时候我们会为事件回调函数添加一个参数(通常是e),并在函数中加入e.preventdefault():以取消默认行为.由于习惯,我顺手将它写到了一个checkbox的change事件中.由于不同的 ...
- rectified units
from: http://en.wikipedia.org/wiki/Rectifier_(neural_networks) In the context of artificial neural n ...