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)
样例输入
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

题意大概:给出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;
}

nyoj 题目283 对称排序相关推荐

  1. 对称排序 nyoj 283

    对称排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 In your job at Albatross Circus Management (yes, it's ru ...

  2. 【数据结构】排序相关题目及各种排序方法的总结

    [数据结构之排序] 常用的排序方法有:直接插入排序.希尔排序.冒泡排序.快速排序.简单选择排序.树形选择排序.堆排序.归并排序.基数排序 提示:如有不理解的知识点,请看B站最好的数据结构老师王卓老师的 ...

  3. 7-47 对称排序 (25 分)

    7-47 对称排序 (25 分) 你供职于由一群丑星作为台柱子的信天翁马戏团.你刚完成了一个程序编写,它按明星们姓名字符串的长度非降序(即当前姓名的长度至少与前一个姓名长度一样)顺序输出他们的名单.然 ...

  4. nyoj_283_对称排序_201312051155

    对称排序 时间限制:1000 ms  |           内存限制:65535 KB 难度:1 描述 In your job at Albatross Circus Management (yes ...

  5. NYOJ283对称排序

    对称排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 In your job at Albatross Circus Management (yes, it's ru ...

  6. 2016年蓝桥杯C语言大学A组题目4--快速排序

    题目4.快速排序 排序在各种场合经常被用到. 快速排序是十分常用的高效率的算法. 其思想是:先选一个"标尺", 用它把整个队列过一遍筛子, 以保证:其左边的元素都不大于它,其右边的 ...

  7. 变态最大值--nyoj题目811

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解决了 ...

  8. 【编程题目】对称子字符串的最大长度 ★

    73.对称字符串的最大长度(字符串). 题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串"google",由于该字符串里最长的对称子字符串是" ...

  9. LeetCode LCS 02. 完成一半题目(计数+排序)

    文章目录 1. 题目 2. 解题 1. 题目 有 N 位扣友参加了微软与力扣举办了「以扣会友」线下活动. 主办方提供了 2*N 道题目,整型数组 questions 中每个数字对应了每道题目所涉及的知 ...

最新文章

  1. oracle insert parallel,insert /*parallel */ 到不同用户,并行起不来的问题
  2. I00031 Look-and-say sequence
  3. svn 迁移到 git 仓库并保留 commit 历史记录
  4. java平台设计zhe_基于java平台的网上评教系统的设计与实现
  5. 【学习笔记】 pytorch的使用语法和代码实例
  6. 2.Redis数据库(搭建redis主从的必要性)以及主从搭建(Windows为例)
  7. DOM树和Render树的创建
  8. SAP云平台里UI5应用的build日志
  9. java学习之(内部类)
  10. AI本质就是“暴力计算”?看华为云如何应对算力挑战
  11. 【zookeeper】zookeeper 的监听机制
  12. Python新式类与经典类(旧式类)的区别
  13. 程序员撕开京东 618 大促压测的另一面 | 原力计划
  14. 3S技术集成与综合应用实习报告(一)
  15. html5 json.stringify,JSON.stringify()妙用
  16. python电力系统分析_PyPSA电力系统分析简介
  17. “用户体验及可用性测试”读后感
  18. HTML教程笔记(菜鸟教程)
  19. Qt开发——圆面积计算器
  20. RFB Net 笔记

热门文章

  1. 小米电视盒服务器维护,告别小白!小米电视/盒子不为人知的设置技巧
  2. 论文笔记:DLWL: Improving Detection for Lowshot classes with Weakly Labelled data
  3. C语言中fclose函数
  4. 学大教育仙林亚东城校区——南京市栖霞区仙隐北路12号亚东商业广场1楼B12
  5. Shopee上新,真的很重要吗?
  6. 阿里刘振飞:聚安全人之力 为全社会赋能
  7. 无线通信模块在物联网中的作用
  8. EOJ 3452 唐纳德先生和假骰子
  9. 广东首例!涉“人脸识别”公民个人信息保护民事公益诉讼案宣判
  10. ThreadX分析(一)