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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值