poj 2013 Symmetric Order 解题报告
题目链接:http://poj.org/problem?id=2013
设长度非递减的字串序列为s[1]...s[n]。设计递归子程序print(n),其中n为字串序号,每分析1个字串,n=n-1。 n = 0 为边界。字串s为局部变量:
先输入和输出当前组的第1个字串s,n = n - 1;
若n > 0,则输入当前组的第2个字符s,n = n - 1。若n > 0,则通过递归调用print(n)将字串s入栈。回溯过程相当于栈顶字串s出栈,因此直接输出s。
#include <iostream>
#include <string>
using namespace std; void print(int n) // 输入n个字串,并按对称格式输出
{
string s; // 当前字串
cin >> s; // 输入和输出当前组的第1个字串
cout << s << endl;
if (--n)
{
cin >> s; // 输入当前组的第2个字串并通过递归压入系统栈区
if (--n)
{
print(n);
}
cout << s << endl; // 回溯,栈首字串出栈后输出
}
} int main()
{
int n, loop = ; // 字串集合序号初始化
while (cin >> n && n)
{
printf("SET %d\n", ++loop);
print(n); // 按照对称格式输出当前字串集合中的n个字串
}
return ;
}
不用递归也可以。对称的输出形式由两部分组成:
上半部分由自上而下的奇数行组成:
s[1]
s[3]
s[5]
......
n 为奇数时为s[n],n为偶数时为s[n-1]
即执行语句 "for (int i = 1; i <= n; i += 2) cout << s[i] << endl; "
下半部分由自下而上的偶数行组成:
s[n - (n%2)]
s[n - (n%2) - 2]
s[n - (n%2) - 4]
......
s[2]
即执行语句" for (int i = n - (n%2); i > 1; i -= 2) cout << s[i] << endl; "。
poj 2013 Symmetric Order 解题报告的更多相关文章
- OpenJudge/Poj 2013 Symmetric Order
1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- [POJ 1002] 487-3279 C++解题报告
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 228365 Accepted: 39826 D ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- 【原创】poj ----- 1611 The Suspects 解题报告
题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS Memory Limit: 20000K To ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
随机推荐
- Excel 计算 tips
1. 对一列数据想看看,distinct的结果 选中数据区域(包含列名),插入pivot table 2. 想检查一个单元格的值在不在某一列中,并返回标志值 =IF (COUNTIF(B:B,A1) ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- Chkrootkit Sourcecode Learning
目录 . Chkrootkit Introduce . Source Code Frame . chklastlog.c . chkwtmp.c . ifpromisc.c . chkproc.c . ...
- Codeforces 650C Table Compression
传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- ubuntu设置开机启动图形应用程序,替换默认图形桌面
直接将启动程序放在rc.local即可.但是如果自动启动的程序奔溃后,会返回到ubuntu的unity桌面系统. 我遇到的问题是程序还有调用 xset 去定时关闭屏幕.在桌面启动后调用没问题.如果rc ...
- Xcode7中新技术
Xcode7真加了两个重要的debug功能:1:Address Sanitizer: 再也不用担心 EXC_BAD_ACCESS 在项目的Scheme中Diagnostics下,选中enable ad ...
- Web SQL数据库
Web SQL数据库:它是一个独立的规范,引入了一组使用SQL操作客户端数据库的API. openDatabase方法:这个方法使用现有的数据库或者新建的数据库创建一个数据库对象.如果数据库存在,op ...
- 繁华模拟赛 Vicent与游戏
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...
- LR监控Windows资源
1.监控准备: 监控方: 1)安装tcp/ip协议下的netbios 2)用administrator登录 被监控方: 1)被监控的Windows开启两个服务: Remote ProcedureCal ...