ACM-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 a higher 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 contains n (1<=n<=1 000), the number of names to follow. Each of the following n lines 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, modiers, and the word class will be separated from 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.
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,middle,lower,upper>middle>lower。以此拓展:upper-upper>middle-upper>lower-upper。但是任意的upper级别的都要比middle级别大(这里是指最后一个量级),比如:lower-upper>upper-middle,middle-middle>upper-lower。
仍有一点需注意:当量级不同的时候,较少量级前面全部默认为middle级。比如题中所说的middle-middle-lower-middle and lower-middle是相同的,因为后者加两个量级后变为middle-middle-lower-middle。当所有的量级相同时,将人名按照字典顺序排序。

解题思路:把upper存为3,middle=2,lower=1,存到结构体的数组里,同时记录下每个人的量级个数。然后进行翻转,并在循环过程中找出最大量级,在不足最大量级的字符串后面补上2。然后进行排序。

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
struct Classy
{
    string name;
    char level[300];
    int logo;//**这里用来记录每个人名的量级个数**
} s[1010];
void Reverse( int max,int n,int i)
{
    int j;
    for(j=0; j<=max/2-1; j++)
    {
        char c;
        c=s[i].level[j];
        s[i].level[j]=s[i].level[max-j-1];
        s[i].level[max-j-1]=c;
    }
}
bool cmp(struct Classy x,struct Classy y)
{
    if(strcmp(x.level,y.level)!=0)
        return strcmp(x.level,y.level)>0;
    return x.name<</span>y.name;
}
int main()
{
    int n;
    while(cin>>n)
    {
        int i,j,k=0;
        string str;
        for(i=0; i<=n-1; i++)
        {
            for(j=0; j<=299; j++)
                s[i].level[j]='2';
        }
        int num=0,Max=0;
        for(i=0; i<=n-1; i++)
        {
            num=0;
            k=0;
            cin>>s[i].name;
 s[i].name.erase(s[i].name.begin()+s[i].name.size()-1,s[i].name.end()); //**因为每个人后面都有一个:,这里用来删去最后一个字符。**
            while(cin>>str)
            {
                if(str=="class")
                    break;
                if(str=="upper")
                    s[i].level[k++]='3';
                if(str=="middle")
                    s[i].level[k++]='2';
                if(str=="lower")
                    s[i].level[k++]='1';
                num++;
            }
            if(num>Max)
                Max=num;    //**这里找出最大的量级数**
            s[i].logo=num;
            Reverse(num,n,i);  //**这里用来翻转量级**
        }
        for(i=0; i<=n-1; i++)
        {
            for(j=s[i].logo; j<=Max-1; j++)
            {
                s[i].level[j]='2';
            }
            s[i].level[j]='\0';
        }
        sort(s,s+n,cmp); //**排序**
        for(i=0; i<=n-1; i++)
            cout<<s[i].name<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值