解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int id;
int score;
struct Node *next;
}Node;
Node* create_node(int id, int score) {
Node *new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) { // 内存分配失败检查
exit(1);
}
new_node->id = id;
new_node->score = score;
new_node->next = NULL;
return new_node;
}
Node* build_list(int n){
Node *head=NULL,*tail=NULL;
int id,score;
for(int i=0;i<n;i++){
scanf("%d %d",&id,&score);
Node *new_node=create_node(id,score);
if(head == NULL){
head=new_node;
tail=new_node;
}
if(head!=NULL){
tail->next=new_node;
tail=new_node;
}
}
return head;
}
void print_list(Node* head){
Node* temp=head;
while(temp){
printf("%d %d\n",temp->id,temp->score);
temp=temp->next;
}
}
Node* merge_and_sort(Node *a,Node *b){
Node *temp;
int count=0;
temp=a;
while(temp){
count++;
temp=temp->next;
}
temp=b;
while(temp){
count++;
temp=temp->next;
}
int ids[count];
int scores[count];
int index=0;
temp=a;
while(temp){
ids[index]=temp->id;
scores[index]=temp->score;
index++;
temp=temp->next;
}
temp=b;
while(temp){
ids[index]=temp->id;
scores[index]=temp->score;
index++;
temp=temp->next;
}
for(int i=0;i<count;i++){
for(int j=0;j<count-i-1;j++){
if(ids[j]>ids[j+1]){
int temp_id=ids[j];
ids[j]=ids[j+1];
ids[j+1]=temp_id;
int temp_score=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp_score;
}
}
}
Node *head=NULL,*tail=NULL;
for(int i=0;i<count;i++){
Node *new_node=create_node(ids[i],scores[i]);
if(head == NULL){
head=new_node;
tail=new_node;
}
if(head!=NULL){
tail->next=new_node;
tail=new_node;
}
}
return head;
}
int main()
{
int n,m;
Node *LA,*LB;
scanf("%d %d",&n,&m);
LA=build_list(n);
LB=build_list(m);
Node *List_result=merge_and_sort(LA,LB);
print_list(List_result);
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复