Description
已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。
Input
第一行,a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成
Output
按照学号升序排列的数据
Sample Input
2 3
5 100
6 89
3 82
4 95
2 10
Sample Output
2 10
3 82
4 95
5 100
6 89
#include <cstdio>
#include <iostream>
#include <cmath>
#include <
string>
#include <cstring>
#include <algorithm>
using namespace std;
struct student
{
int score, id;
}a[1000+
8];
bool compare(student x, student y)
{
return x.id<
y.id;
}
int main()
{
int n, m;
scanf("%d%d", &n, &
m);
for(
int i =
0; i<n+m; i++
)
{
scanf("%d%d", &a[i].id, &
a[i].score);
}
sort(a, a+(n+
m), compare);
for(
int i =
0; i<n+m; i++
)
{
cout << a[i].id <<
' '<< a[i].score<<
endl;
//printf("%d %d\n", a[i].id, a[i].score);
}
return 0;
}
转载于:https://www.cnblogs.com/RootVount/p/10370513.html