nyoj 题目283 对称排序

来源:互联网 发布:乔丹98年总决赛数据 编辑:程序博客网 时间:2024/06/01 08:22


http://acm.nyist.net/JudgeOnline/problem.php?pid=283

对称排序

时间限制: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)
样例输入
7BoPatJeanKevinClaudeWilliamMarybeth6JimBenZoeJoeyFrederickAnnabelle5JohnBillFranStanCece0
样例输出
SET 1BoJeanClaudeMarybethWilliamKevinPatSET 2JimZoeFrederickAnnabelleJoeyBenSET 3JohnFranCeceStanBill

题意大概:给出N个单词,把单词按照一定规律输出,即按照长度,最短的放第一位,第二短的放最后一位,然后再短第放到第二位。。。这样输出。

解法:先把单词和单词的长度放到结构体中,然后按长度正排(按长度从小到大排),然后按照上面的规律放到二维数组a[][]中,然后输出。



#include<stdio.h>#include<string.h>#include<stdlib.h>struct Word{char s[100];int n;}num[1000];int cmp(const void *a,const void *b){return ((Word *)a)->n-((Word *)b)->n;}int main(){int N,i,j,k,t=1;char a[1000][100];while(scanf("%d",&N)&&N!=0){for(i=0;i<N;i++){    scanf("%s",num[i].s);    num[i].n=strlen(num[i].s);}qsort(num,N,sizeof(num[0]),cmp);//按字符串长度对单词进行顺排序(从小到大) for(i=0;i<N;i++)memset(a[i],'\0',sizeof(a[i]));for(i=0,j=N-1,k=0;k<N;k++){//将单词按规律放到二维数组中 if(k%2==0)strcpy(a[i++],num[k].s);  elsestrcpy(a[j--],num[k].s);        //按照先在最上边放一个,再在最下边放一个的规律放 }printf("SET %d\n",t++);for(i=0;i<N;i++)printf("%s\n",a[i]);}return 0;}

0 0
原创粉丝点击