对称排序

对称排序

描述

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
 
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b)
{
return a.length()<b.length();
}
int main(){
string s[20],temp;
int i,j,n,t=0;
while(cin>>n&&n){
t++;
for(i=0;i<n;i++)cin>>s[i];
sort(s,s+n,cmp);
cout<<"SET "<<t<<endl;
for(i=0;i<n;i++)if(i%2==0)cout<<s[i]<<endl;
for(i=n-1;i>=1;i--)if(i%2!=0)cout<<s[i]<<endl;
}
return 0;
}        

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • VF

    Vasya is the beginning mathematician. He decided to make an important contributi...

    书童小二
  • Binary String Matching

    Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is...

    书童小二
  • 前端神器之Sublime Text2/3简单明了使用总结

    第一:也是最重要的,它占内存很小(就如同notepad++那般迅速打开,所以那款其实也不错~)。一般IDE比如WebStorm(它也是一款神器来着),Aptan...

    书童小二
  • Flexbox 练习和总结

    练习地址: http://flexboxfroggy.com/ Welcome to Flexbox Froggy, a game where you help...

    SmileSmith
  • numpy.geomspace

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明...

    于小勇
  • From High Ceph Latency to Kernel Patch with eBPF/BCC

    There are a lot of tools for debugging kernel and userspace programs in Linux. M...

    Linux阅码场
  • 3D角色设定全析百图斩

    ? 我们在3D角色模型与动画设定上积累厚实,在卡噗角色设定中,我们做了很多的风格尝试以及整体配饰穿搭,去达到新锐时尚的品牌目标。回看角色设定已超过100体,在...

    腾讯ISUX
  • What are x509 certificates? RFC? ASN.1? DER?

    So, RFC means Request For Comments and they are a bunch of text files that descr...

    战神伽罗
  • AMD Zen-2 物理设计总结

    Last year AMD introduced the Zen 2 microarchitecture, the company’s second major...

    白山头
  • U-Net - Convolutional Networks 论文翻译——中英文对照

    翻译论文汇总:https://github.com/SnailTyan/deep-learning-papers-translation

    Tyan

扫码关注云+社区

领取腾讯云代金券