2015-2016 ACM-ICPC Class Time

题目地址:http://codeforces.com/gym/100819/attachments

题目:

It’s the first day of class! Tom is teaching class and first has to take attendance to see who is inclass. He needs to call the students’ names in alphabetical order by last name. If two students havethe same last name, then he calls the students with that same last name in alphabetical order byfirst name. Help him!

Input

The first line of input contains an integer n (1 ≤ n ≤ 100), the number of students in Tom’s class.Each of the following n lines contains the name of a single student: first name, followed by a singlespace, then last name. The first and last name both start with an uppercase letter (‘A’–‘Z’) andthen be followed by one or more lowercase letters (‘a’–‘z’). The first and last name of each studentis no more than 10 letters long each.It is guaranteed that no two students have exactly the same name, though students may sharethe same first name, or the same last name.

Output

Output n lines, the names of the students as Tom calls them in the desired order.


思路:

这题的坑是先比较后面的字符串,再比较前面的。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct st
{
    string a;
    string b;
}name[101],na[101];
bool cmp(st z,st x)
{
    if(z.b==x.b)return z.a>x.a;
    return z.b>x.b;
}
string zz(string a)
{
    int c=a.length();
    int i;
    char b[11];
    for(i=0;i<c;i++)
    {
        if(a[i]<'a')b[i]=a[i]+('a'-'A');
        else b[i]=a[i];
    }
    string d;
    for(i=0;i<c;i++)
    {
        d=d+b[i];
    }
    return d;
}
string zs(string a)
{
    int c=a.length();
    int i;
    char b[11];
    for(i=0;i<c;i++)
    {
        if(i==0)b[i]=a[i]-('a'-'A');
        else b[i]=a[i];
    }
    string d;
    for(i=0;i<c;i++)
    {
        d=d+b[i];
    }
    return d;
}
int main()
{
    int n;
    while(cin>>n)
    {
        int i;
        for(i=0;i<n;i++)
        {
            cin>>name[i].a>>name[i].b;
            na[i].a=zz(name[i].a);
            na[i].b=zz(name[i].b);
        }
        sort(na,na+n,cmp);
        for(i=n-1;i>=0;i--)
        {
            cout<<zs(na[i].a)<<" "<<zs(na[i].b)<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zero_979/article/details/80738909