对称排序acm283
2016-05-24 19:29
295 查看
对称排序时间限制: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 asthe 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 namesbelongs 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. Noneof 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个单词,把单词按照一定规律输出,即按照长度,最短的放第一位,第二短的放最后一位,然后再短第放到第二位。。。这样输出。需要重新sort一下按长度排好序,然后每隔一个输出
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; struct node { int l; char s[40]; } a[1000]; bool cmp(struct node a,struct node b) { return a.l<b.l; } int main() { int n; int g=1; while(scanf("%d",&n)!=-1) { if(n==0) { break; } int l=0; for(int i=0; i<n; i++) { scanf("%s",a[i].s); a[i].l=strlen(a[i].s); } sort(a,a+n,cmp); printf("SET %d\n",g++); for(int i=0; i<n; i+=2) { printf("%s\n",a[i].s); if(i==n-1) { for(int j=n-2; j>=0; j-=2) { printf("%s\n",a[j].s); } } if(i==n-2) { for(int k=n-1; k>=0; k-=2) { printf("%s\n",a[k].s); } } } } }
相关文章推荐
- 【cocos2d-x 手游研发----怪物智能AI】
- 浅谈Handler机制
- java,你是谁?
- Ubuntu-16.04安装jdk
- Linux环境下软件安装
- python 中time.sleep没有作用
- 视屏暂停的时候添加广告
- 【cocos2d-x 手游研发小技巧 循环无限滚动的登陆背景】
- Activity中 findViewById 获取控件对象正确的代码位置
- 2016"百度之星" - 初赛(Astar Round2A)Sitting in Line
- Shell操作
- django 2
- 浅析双绞线中五类线每根线的功能
- Android Fragment 深入理解
- C++ STL算法系列--copy函数
- S number(2016黑龙江省赛J题)
- HDU 4685 强联通分量+网络流
- 修改 Ubuntu 源(清华大学)
- Linux网络设置
- 【问题】sql数据库安装重启失败