zoukankan      html  css  js  c++  java
  • 【基础题】对称排序

    问题 : 对称排序

    时间限制: 1 Sec  内存限制: 128 MB
    [提交][状态][讨论版]

    题目描述

    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

    题目大意:
    给你几个字符串,wants the output to appear more symmetric,就是让这几句字符串输出时中间长两边短。
    思路:
    将给出几句字符串用stable_sort()按升序排序,然后先输出奇数的字符串,再输出偶数的字符串,如n=7,则输出
    1,3,5,7,6,4,2;n=6则输出1,3,5,6,4,2。

    代码如下:
     #include<stdio.h>
        #include<stdlib.h>
        #include<string.h>
        #include<algorithm>
        using namespace std;
        typedef struct Px
        {
            char s[30];
        }Word;
        bool cmp(Word x,Word y)
        {
            int l1,l2;
            l1=strlen(x.s);
            l2=strlen(y.s);
            if(l1<l2)
            return 1;
            else return 0;
        }
        int main()
        {
            int t,x=1;
            Word a[20];
            while(scanf("%d",&t),t)
            {
                memset(a,0,sizeof(a));
                int i;
                for(i=0;i<t;i++)
                 scanf("%s",a[i].s);
                stable_sort(a,a+t,cmp);
                printf("SET %d
    ",x);
                for(i=0;i<t;i+=2)
                {
                    printf("%s
    ",a[i].s);
                }
                if(t&1)
                t=t-1;
                for(i=t-1;i>=1;i-=2)
                {
                    printf("%s
    ",a[i].s);
                }
                x++;
            }
            return 0;
        }

    反思:

    我的思路是把奇数字符串分给一个数组,偶数的字符串分给一个数组,然后用stable_sort()给两个数组一个升序一个降序,显然我这个方法很麻烦,而且,当遇到如第三组测试数据时,每个字符串的长度相等,所以排序后偶数数组并没有变化,输出仍是Bill在前,Still在后,而上面的思路,不但效率高,而且解决了这个问题。

    该题用到的C++知识点:

    1、sort()排序函数的使用:

    传送门:http://blog.csdn.net/zzzmmmkkk/article/details/4266888/

    2、当用到字符串数组时,可以先考虑,结构体数组。

    3、C/C++中strlen()详解和strlen()与sizeof()的区别:

    传送门:http://blog.csdn.net/smf0504/article/details/51372351

    4、C/C++中memset();的用法;

    传送门:http://blog.csdn.net/binnygoal/article/details/6460887



    祝你早日攒够失望,然后开始新的生活。
  • 相关阅读:
    讨喜的隔离可变性(十三)角色的特性
    讨喜的隔离可变性(十三)角色的特性
    解锁不可见索引新特性,处理ORA-01555故障
    django url 正则匹配
    django 往session写信息
    django 判断用户是否登录
    django 管理session
    Python爬虫入门教程 1-100 CentOS环境安装
    django session管理
    案发现场:被注入的软件及 ORA-600 16703 灾难的恢复
  • 原文地址:https://www.cnblogs.com/LuRenJiang/p/6972796.html
Copyright © 2011-2022 走看看