题目链接: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 解题报告的更多相关文章

  1. OpenJudge/Poj 2013 Symmetric Order

    1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...

  2. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  3. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  4. [POJ 1002] 487-3279 C++解题报告

        487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 228365   Accepted: 39826 D ...

  5. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

  6. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  7. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  8. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  9. [POJ 1001] Exponentiation C++解题报告 JAVA解题报告

        Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 126980   Accepted: 30 ...

随机推荐

  1. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. android4.0浏览器在eclipse中编译的步骤

    工程源码: 注意: 如果下载已经修过的源码,只要进行3.4.8步骤就应该可以了. eclipse版本:adt-bundle-windows (Android Developer Tools Build ...

  3. js中数组以及for循环的使用

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  4. Oracle 11g ORA-00845: MEMORY_TARGET not supported on this system

    启动Oracle 11gR2后报错:ORA-00845 rac1:/home/oracle> sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 ...

  5. 51nod 1170 1770 数数字(动态规划)

    解题思路:看到题后,直接想到分成两种情况: ①:a*b >9 这里又分成两种 1. n==1 a*b 直接是一个两位数 求得十位和个位(这里十位和个位不可能相等) 然后如果等于d 则结果=1 2 ...

  6. MySQL日期数据类型、MySQL时间类型使用总结

    MySQL:MySQL日期数据类型.MySQL时间类型使用总结 MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 ------------ --- ...

  7. 我的前端MVC之路

    大约十几个月前,了解到时下前端MVC之火爆,同事推荐我了解一下angular.当时也不是特别在意,只是稍稍阅读了一遍官方文档,并尝试了文档上的例子.其实当时也颇有震惊之感的,原来代码还可以这么写!看完 ...

  8. [原]Android官方图片加载利器BitmapFun解析

    通过BitmapFun在项目中使用,结合代码了解一下BitmapFun加载图片的原理,以及最佳使用实践.本文说明不包括BitmapFun的缓存部分. Android开发在使用ListView和Grid ...

  9. 一张图解释Hadoop IPC

    基于hadoop2.6.2.... 一张图Server启动,Client访问..... RPC是IPC的一种,IPC还有另外一种LPC,相关请看参考中的3 使用hadoop ipc步骤: 1.定义RP ...

  10. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...