对称排序
描述
Inyour 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 innondescending order by length (so that each name is at least as long as the onepreceding it). However, your boss does not like the way the output looks, andinstead wants the output to appear more symmetric, with the shorter strings atthe top and bottom and the longer strings in the middle. His rule is that eachpair of names belongs on opposite ends of the list, and the first name in thepair is always in the top part of the list. In the first example set below, Boand Pat are the first pair, Jean and Kevin the second pair, etc.
输入
Theinput consists of one or more sets of strings, followed by a final linecontaining 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 perline, NOT SORTED. None of the strings contain spaces. There is at least one andno more than 15 strings per set. Each string is at most 25 characters long.
输出
Foreach input set print "SET n" on a line, where n starts at 1, followedby 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
代码如下:
#include<stdio.h>//最优程序
#include<string.h>
#include<stdlib.h>
main()
{
int x,i,j,q=0;
char s[16][26],t[26],z[16][26];
while(1)
{
scanf("%d",&x);//单词的个数
if(x==0) break;
q++;
getchar();
for(i=0;i<x;i++)
gets(s[i]);//先输入7个完整的单词 存入数组s
for(i=1;i<x;i++)
for(j=0;j<x-i;j++)
if(strlen(s[j])>strlen(s[j+1])) //第一个长度大于第二个长度
{
strcpy(t,s[j]);//将字符串长度大的的存在数组t中
strcpy(s[j],s[j+1]);//
strcpy(s[j+1],t);
}
//以上排序完成
for(i=0,j=0;i<x;i++,j++)
{
strcpy(z[j],s[i]);
if(i!=x-1)
strcpy(z[x-j-1],s[++i]);
}
printf("SET %d\n",q);
for(i=0;i<x;i++)
puts(z[i]);
}
}