对称排序
时间限制:
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.
}