对称排序

  1 /* 
  2 对称排序
  3 时间限制:1000 ms  |  内存限制:65535 KB
  4 难度:1
  5 描述
  6 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
  7 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 
  8 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 
  9 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 
 10 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. 
 11 输入
 12 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 
 13 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 
 14 contain spaces. There is at least one and no more than 15 strings per set. Each string is at most 25 characters long. 
 15 输出
 16 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.
 17 If length of two strings is equal,arrange them as the original order.(HINT: StableSort recommanded)
 18 样例输入
 19 7
 20 Bo
 21 Pat
 22 Jean
 23 Kevin
 24 Claude
 25 William
 26 Marybeth
 27 6
 28 Jim
 29 Ben
 30 Zoe
 31 Joey
 32 Frederick
 33 Annabelle
 34 5
 35 John
 36 Bill
 37 Fran
 38 Stan
 39 Cece
 40 0
 41 样例输出
 42 SET 1
 43 Bo
 44 Jean
 45 Claude
 46 Marybeth
 47 William
 48 Kevin
 49 Pat
 50 SET 2
 51 Jim
 52 Zoe
 53 Frederick
 54 Annabelle
 55 Joey
 56 Ben
 57 SET 3
 58 John
 59 Fran
 60 Cece
 61 Stan
 62 Bill
 63 来源
 64 POJ
 65 上传者
 66 sadsad
 67 */
 68 #include<stdio.h>
 69 #include<string.h>
 70 int main()
 71 {
 72     char s[16][26],c[16][26],a[26];
 73     int n, i, j, k,t=1;
 74     while(scanf("%d",&n) != EOF && n)
 75     {
 76         getchar();
 77         for(i=0; i<n; i++)
 78         gets (s[i]);
 79         //对字符串按长度排序 
 80         for(i = 0; i < n-1;i++)
 81         {
 82             for(j = 0 ;j < n-i-1 ; j++)
 83             {
 84                 memset(a,'\0',26);
 85                 if(strlen(s[j]) > strlen(s[j+1]) )
 86                 {
 87                     strcpy(a,s[j]);
 88                     strcpy(s[j],s[j+1]);
 89                     strcpy(s[j+1],a);
 90                 }
 91             }
 92         }
 93         //按题目要求输出 
 94         j=k=0;
 95         printf("SET %d\n",t++);
 96         for(i=0; i<n; i++)
 97         {
 98             if(i%2==0)
 99             {
100             strcpy(c[j],s[i]);
101             j++;
102             }
103             else
104             {
105             strcpy(c[n-1-k],s[i]);
106             k++;
107             }
108         }
109         for(i=0; i<n; i++)
110         printf("%s\n",c[i]);
111     }
112     return 0;
113 }

 

转载于:https://www.cnblogs.com/a604378578/p/3658912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值