Class Time(水题,快排)

Description

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.

Sample Input

3

John Adams

Bob Adam

Bob Adams

Sample Output

Bob Adam

Bob Adams

John Adams

Sample Input

1

Coursera Educators

Sample Output

Coursera Educators

题解:emmmmm.....,没啥好说的。

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
struct pop
{
	char a[11];
	char b[11];
}s[111];
bool cmp(struct pop p,struct pop q)
{
    if(strcmp(p.b,q.b)==0)
        return strcmp(p.a,q.a)<0;
    return strcmp(p.b,q.b)<0;
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
	{
		cin>>s[i].a>>s[i].b;
	}
	sort(s,s+n,cmp);
	for(int i=0;i<n;i++)
	{
		cout<<s[i].a<<" "<<s[i].b<<endl;
	}
    return 0;
}











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值