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