对称排序
时间限制: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
- 来源
- POJ
-
上传者
题目大意:意思是你已经写好了一个程序,可惜你的程序输出方式老板不喜欢!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.老板希望你能“symmetric”匀称的输出!所以刚开始你就必须认为输入的字符串的长度是和顺序毫无关联的!理解到这里,题目差不多就已经解出来了!即先把字符串排好序,短的在上长的在下,然后“symmetric”的输出就OK了!本题题意是先将输入的字符串进行由短到长排序后在进行调换输出。是奇数个则输出1
3 5 7 6 4 2。是偶数个则输出1 3 5 6 4 2。#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; typedef struct Px { char s[30]; }Word; bool cmp(Word x,Word y) { int l1,l2; l1=strlen(x.s); l2=strlen(y.s); if(l1<l2) return 1; else return 0; } int main() { int t,x=1; Word a[20]; while(scanf("%d",&t),t) { memset(a,0,sizeof(a)); int i; for(i=0;i<t;i++) scanf("%s",a[i].s); sort(a,a+t,cmp); printf("SET %d\n",x); for(i=0;i<t;i+=2) { printf("%s\n",a[i].s); } if(t&1) t=t-1; for(i=t-1;i>=1;i-=2) { printf("%s\n",a[i].s); } x++; } return 0; }