描述
已有a、b两个链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。
输入
第一行,a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成
输出
按照学号升序排列的数据
输入样例 1
2 3
5 100
6 89
3 82
4 95
2 10
输出样例 1
2 10
3 82
4 95
5 100
6 89
这题表面上是说链表但是有了c++我们其实可以使用对象数组,使用类,使用对象数组存储所有数据,然后再对学号排序即可
#include <iostream>
#include <cstring>
using namespace std;
class STUDENT
{
public:
int num;
int score;
STUDENT(){};
STUDENT(int a,int b)
{
num=a;
score=b;
}
void display()
{
cout<<num<<" "<<score<<endl;
}
};
void SORT(STUDENT a[],int n)
{
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(a[i].num>a[j].num)
{
STUDENT temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int main()
{
int a,b;
while(cin>>a>>b)
{
int n=a+b,x,y;
STUDENT a[n];
for(int i=0;i<n;i++)
{
cin>>x>>y;
a[i]=STUDENT(x,y);
}
SORT(a,n);
for(int i=0;i<n;i++)
a[i].display();
}
return 0;
}