Classy(排序)

Description

In his memoir So, Anyway. . ., comedian John Cleese writes of the class di erence between his father(who was "middle-middle-middle-lower-middle class") and his mother (who was  "upper-upper-lower-middle class"). These fine distinctions between classes tend to confuse American readers, so you are to write a program to sort a group of people by their classes to show the true distinctions.

There are three main classes: upper, middle, and lower. Obviously, upper class is the highest,and lower class is the lowest. But there can be distinctions within a class, so upper-upper is ahigher class than middle-upper, which is higher than lower-upper. However, all of the upper classes(upper-upper, middle-upper, and lower-upper) are higher than any of the middle classes.

Within a class like middle-upper, there can be further distinctions as well, leading to classes like lower-middle-upper-middle-upper. When comparing classes, once you‘ve reached the lowest level of detail, you should assume that all further classes are the equivalent to the middle level of  the previous level of detail. So upper class and middle-upper class are equivalent, as are middle-middle-lower-middle and lower-middle.

Input

The rst line of input containsn(1<=n<=1 000), the number of names to follow. Each of the followingnlines contains the name of a person (a sequence of 1 or more lowercase letters `a‘-`z‘),a colon, a space, and then the class of the person. The class of the person will include one or more modifiers and then the word class. The colon, modi ers, and the wordclasswill be separatedfrom each other by single spaces. All modifiers are one of upper,middle, or lower. It is guaranteed that the input is well-formed. Additionally, no two people have the same name. Input lines are no longer than 256 characters.

Output

Print the n names, each on a single line, from highest to lowest class. If two people have equivalent classes, they should be listed in alphabetical order by name.

Examples

//Sample Input
5
mom: upper upper lower middle class
dad: middle middle lower middle class
queenelizabeth: upper upper class
chair: lower lower class
unclebob: middle lower middle class

10
rich: lower upper class
mona: upper upper class
dave: middle lower class
charles: middle class
tom: middle class
william: lower middle class
carl: lower class
violet: middle class
frank: lower class
mary: upper class

//Sample Output
queenelizabeth
mom
dad
unclebob
chair

mona
mary
rich
charles
tom
violet
william
carl
dave
frank

题意:

已知班级有上中下之分,班级里又有上中下之分,以此类推。现在给出多个人以及他们所在的班级,要求按照从上到下的顺序给他们排序。

给定的所在班级的顺序为从后到前排列,例如 upper lower class是低于lower  upper  class(先比较class之前的,以此往前推)。

如果没有排列,默认为中等,例如upper class是高于lower  upper  class(upper class相当于middle upper  class)。

如果两个班级相等,按照姓名的字典序排序。

思路:

简单起见,可以将upper标记为3,middle标记为2,lower标记为1,数组初始化为2,方便用strcmp排序。

从前往后便利数组的时候,最后要将得到的标记的数组翻转一下。

代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
struct node
{
    char name[100];
    char lever[100];
    char le[300];
} z[1100];

using namespace std;

bool cmp(struct node a,struct node b)
{
    if(strcmp(a.le,b.le)==0)//如果标记的数组相等
        return strcmp(a.name,b.name)<0;//按照姓名排序
    else
        return (strcmp(a.le,b.le)>0);//否则按照标记的排序
}

void Reverse(char *s,int n)//字符串翻转
{
    for(int i=0,j=n-1; i<j; i++,j--)
    {
        char c=s[i];
        s[i]=s[j];
        s[j]=c;
    }
}

int main()
{
    int n ;
    int logo=0;
    int flag=0;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
        {
            logo=0;
            flag=0;
            scanf("%s",z[i].name);
            int str = strlen(z[i].name);
            z[i].name[str-1] = 0;//去掉冒号
            for(int j=0; j<300; j++)
            {
                if(j==299)
                    z[i].le[j]=0;
                else
                    z[i].le[j]=‘2‘;
            }
            while(1)
            {
                flag++;
                scanf("%s",z[i].lever);
                if(strcmp("upper",z[i].lever)==0)
                    z[i].le[logo++]=‘3‘;
                if(strcmp("middle",z[i].lever)==0)
                    z[i].le[logo++]=‘2‘;
                if(strcmp("lower",z[i].lever)==0)
                    z[i].le[logo++]=‘1‘;
                if(strcmp("class",z[i].lever)==0)
                    break;
            }
            Reverse(z[i].le,flag-1);
        }

        sort(z,z+n,cmp);

        for(int i=0; i<n; i++)
        {
            printf("%s",z[i].name);
            printf("\n");
        }
    }
}
时间: 2025-01-01 03:10:57

Classy(排序)的相关文章

LA UVaLive 7370 Classy (排序,比较)

题意:给定 n 个人,和他们的数进行比较,从后面开始比,如果不够长,加middle接着比,直到没有,如果还相同比名字. 析:很水的题,就不用说了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <functional> #include <cstdlib> #include

Codeforces 1036C Classy Numbers 【DFS】

<题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R]  (1≤L≤R≤1018).,问你这个区间内有多少个classy number 数. 解题分析: 对于这种对数的数位有要求的题目,可以往搜索,dp上想一想,本题可用搜索做.先用搜索将所有符合条件的数放入vector ,然后排序,再用二分函数得到 L,R的坐标,再相减,即可得到 [L,R]区间中满足条件的数的个数. #include <bits/std

经典排序算法 - 冒泡排序Bubble sort

 原文出自于 http://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html 经典排序算法 - 冒泡排序Bubble sort 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子为从小到大排序, 原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 | 第一趟排序(外循环) 第

算法 希尔排序

希尔排序 Shell Sort 介绍: 希尔排序(Shell Sort)也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本. 该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个"增量"的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序(增量足够小)时,再对全体元素进行一次直接插入排序.因为直接插入排序在元素基本有序的情况下(接近最好情况),效率是很高的,因此希尔排序在时间效率比直接插入排序有较大提高. 执行流程: 首先

算法 排序NB二人组 堆排序 归并排序

参考博客:基于python的七种经典排序算法     常用排序算法总结(一) 序前传 - 树与二叉树 树是一种很常见的非线性的数据结构,称为树形结构,简称树.所谓数据结构就是一组数据的集合连同它们的储存关系和对它们的操作方法.树形结构就像自然界的一颗树的构造一样,有一个根和若干个树枝和树叶.根或主干是第一层的,从主干长出的分枝是第二层的,一层一层直到最后,末端的没有分支的结点叫做叶子,所以树形结构是一个层次结构.在<数据结构>中,则用人类的血统关系来命名,一个结点的分枝叫做该结点的"

数字在排序数组中出现的次数

题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 程序实现: import java.util.Scanner; public class Test38 { public static void main(String[] args) { /**  * 初始化数组,测试数据可以多种途径初始化  */   Scanner in = new Scanner(System.in); /*int[] a = 

算法 排序lowB三人组 冒泡排序 选择排序 插入排序

参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一.排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法. 排序的稳定性: 经过某种排序后,如果两个记录序号同等,且两者在原无序记录中的先后秩序依然保持不变,则称所使用的排序方法是稳定的,反之是不稳定

C/C++算法竞赛入门经典Page11 例题1-5 三整数排序

题目:输入3个整数,从小到大排序后输出 样例输入: 20 7 33 样例输出: 7 20 33 首先,先声明三个整数a,b,c和一个临时变量t: int a,b,c,t;//1,b,c为三个整数,t为临时变量 输入三个整数: scanf("%d%d%d",&a,&b,&c); 进行3次if判断: 1.如果b>a,则a,b对调 2.如果c>a,则a,c对调 3.如果c>b,则b,c对调 代码: if(b>=a){ t=b; b=a; a=t

排序与查找

1.冒泡法排序 2.选择排序 3.二分法查找(有序数组) 4.无序数组(基本查找)