对称排序

描述
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

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main(){
 5     char name[20][30];
 6     char temp[20][30];
 7     char compare[30];
 8     int id[20];
 9     int int_temp;
10     int n;
11     int i;
12     int j;
13     int k;
14     int time;
15     int length1;
16     int length2;
17     
18     time=1;
19     
20     while(1){
21         scanf("%d",&n);
22         
23         if(n==0)
24             break;
25         
26         for(i=0;i<n;i++)
27             id[i]=i;
28                 
29         for(i=0;i<n;i++){
30             scanf("%s",&name[i]);
31         }
32         
33         for(i=0;i<n-1;i++){
34             for(j=i+1;j<n;j++){  //主要是参与比较的是长度,当两个字符串长度一样时值却可能不一样,所以要进行标记位置 
35                 length1=strlen(name[i]);
36                 length2=strlen(name[j]);
37                 
38                 if(length1>length2 || (length1==length2 && id[i]>id[j])){
39                     strcpy(compare,name[i]);
40                     strcpy(name[i],name[j]);
41                     strcpy(name[j],compare);
42                     
43                     int_temp=id[i];
44                     id[i]=id[j];
45                     id[j]=int_temp;
46                 }
47             }
48         }
49         
50         j=0;
51         k=n-1;    
52         for(i=0;i<n;i++){    
53             if((i+1)%2==1){
54                 strcpy(temp[j],name[i]);
55                 j++;
56             }
57             
58             else{
59                 strcpy(temp[k],name[i]);
60                 k--;
61             }
62         }
63         
64         printf("SET %d\n",time);
65         time++;
66         
67         for(i=0;i<n;i++)
68             printf("%s\n",temp[i]);
69     }    
70     return 0;
71 }

 

 
posted @ 2014-11-15 17:55  zqxLonely  阅读(394)  评论(0编辑  收藏  举报