对称排序

简介:

对称排序

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述
In your job at Albatross Circus Management (yes, it's run by a bunch of clowns), you have just finished writing a program whose output is a list of names in nondescending order by length (so that each name is at least as long as the one preceding it). However, your boss does not like the way the output looks, and instead wants the output to appear more symmetric, with the shorter strings at the top and bottom and the longer strings in the middle. His rule is that each pair of names belongs on opposite ends of the list, and the first name in the pair is always in the top part of the list. In the first example set below, Bo and Pat are the first pair, Jean and Kevin the second pair, etc.
输入
The input consists of one or more sets of strings, followed by a final line containing only the value 0. Each set starts with a line containing an integer, n, which is the number of strings in the set, followed by n strings, one per line, NOT SORTED. None of the strings contain spaces. There is at least one and no more than 15 strings per set. Each string is at most 25 characters long.
输出
For each input set print "SET n" on a line, where n starts at 1, followed by the output set as shown in the sample output.
If length of two strings is equal,arrange them as the original order.(HINT: StableSort recommanded)
样例输入
7
Bo
Pat
Jean
Kevin
Claude
William
Marybeth
6
Jim
Ben
Zoe
Joey
Frederick
Annabelle
5
John
Bill
Fran
Stan
Cece
0
样例输出
SET 1
Bo
Jean
Claude
Marybeth
William
Kevin
Pat
SET 2
Jim
Zoe
Frederick
Annabelle
Joey
Ben
SET 3
John
Fran
Cece
Stan
Bill

查看代码---运行号:252342----结果:Accepted

运行时间: 2012-10-05 16:35:20  |  运行人: huangyibiao
01. #include <iostream>
02. #include <string>
03. #include <vector>
04. using namespace std;
05.  
06. int main()
07. {
08. int t;
09. string tmp;
10. int set = 1;
11. while (cin >> t && t != 0)
12. {
13. int i = 1;
14. vector<string> vEven, vOdd, v;
15.  
16. for (i = 0; i < t; i++)
17. {
18. cin >> tmp;
19. v.push_back(tmp);
20. }
21. for (i = 0; i < t; i++)
22. {
23. for (int j = 0; j < t-i-1; j++)
24. {
25. if (v[j].size() > v[j+1].size())
26. {
27. string s = v[j];
28. v[j] = v[j+1];
29. v[j+1] = s;
30. }
31. }
32. }
33. for (i = 1; i <= t; i++)
34. {
35. if (i % 2)//奇数位置
36. vOdd.push_back(v[i-1]);
37. else
38. vEven.push_back(v[i-1]);
39. }
40. cout << "SET " << set++ << endl;
41. for (vector<string>::iterator it = vOdd.begin(); it != vOdd.end(); it++)
42. {
43. cout << *it << endl;
44. }
45. for (vector<string>::reverse_iterator it = vEven.rbegin(); it != vEven.rend(); it++)
46. {
47. cout << *it << endl;
48. }
49.  
50. }
51. return 0;
52. }

目录
相关文章
|
7月前
【每日一题Day340】LC2251花期内花的数目 | 差分+哈希表+排序 排序+二分查找
【每日一题Day340】LC2251花期内花的数目 | 差分+哈希表+排序 排序+二分查找
38 0
|
6月前
|
人工智能 C++
组合+排列 以及伯努利装错信封问题思路
这段代码是C++实现的一个程序,用于计算从`n`个不同元素中选择`m`个进行排列的组合总数(排列问题)。用户输入`n`和`m`,程序通过循环和条件判断生成所有可能的排列,并输出排列的总数。核心逻辑是使用回溯法,当找到一个满足条件(不包含重复元素)的排列时,更新计数器并继续寻找下一个排列。
45 0
|
7月前
|
算法
常见的算法排序(2)
常见的算法排序(2)
53 3
|
7月前
|
存储 算法 容器
排列对称串
排列对称串
34 1
|
7月前
|
存储 Python
处理随机元素来创建数列是一个涉及随机数生成和数列构造的过程
处理随机元素来创建数列是一个涉及随机数生成和数列构造的过程
65 0
|
7月前
|
算法 测试技术 C++
【动态规划】【C++算法】801. 使序列递增的最小交换次数
【动态规划】【C++算法】801. 使序列递增的最小交换次数
|
7月前
|
算法 前端开发
前端算法-对称二叉树
前端算法-对称二叉树
|
存储 缓存 C++
C++/PTA 对称排序
你供职于由一群丑星作为台柱子的信天翁马戏团。你刚完成了一个程序编写,它按明星们姓名字符串的长度非降序(即当前姓名的长度至少与前一个姓名长度一样)顺序输出他们的名单。然而,你的老板不喜欢这种输出格式
77 0
|
算法 搜索推荐 Java
算法之冒牌排序
冒泡排序算法及其优化算法
算法之冒牌排序
7-47 对称排序 (25 分)
7-47 对称排序 (25 分)
130 0