解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int num;
int score;
struct node *next;
} node_t;
typedef struct list
{
struct node *head;
struct node *tail;
} list_t;
node_t *creat_node(int num, int score)
{
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->next = NULL;
new_node->num = num;
new_node->score = score;
return new_node;
}
void init_list(list_t *list)
{
list->head = (node_t *)malloc(sizeof(node_t));
list->tail = (node_t *)malloc(sizeof(node_t));
memset(list->head, 0, sizeof(node_t));
memset(list->tail, 0, sizeof(node_t));
list->tail->num=0;
list->head->next = list->tail;
list->tail->next = NULL;
}
void tail_insert(list_t *list, int num, int score)
{
node_t *new_node = creat_node(num, score);
for (node_t *pnode = list->head; pnode != list->tail; pnode = pnode->next)
{
node_t *first = pnode;
node_t *mid = first->next;
node_t *last = mid->next;
if (mid == list->tail)
{
first->next = new_node;
new_node->next = list->tail;
}
}
}
void print(list_t *list)
{
node_t *pnode = list->head->next;
while (pnode->next != NULL)
{
printf("%d %d\n", pnode->num, pnode->score);
pnode = pnode->next;
}
}
void link_list(list_t *list1, list_t *list2)
{
for (node_t *pnode = list1->head; pnode != list2->tail; pnode = pnode->next)
{
node_t *first = pnode;
node_t *mid = first->next;
node_t *last = mid->next;
if (mid == list1->tail)
{
first->next = list2->head->next;
free(mid);
}
}
}
void list_sort(list_t *list)
{
int min, num, score;
for (node_t *pnode = list->head; pnode->next->next != NULL; pnode = pnode->next)
{
min = pnode->next->num;
for (node_t *pnode2 = pnode->next; pnode2->next != NULL; pnode2 = pnode2->next)
{
node_t *mid = pnode2->next;
if (mid->num < min && (mid->next != NULL))
{
min = mid->num;
score = mid->score;
mid->num = pnode->next->num;
pnode->next->num = min;
mid->score = pnode->next->score;
pnode->next->score = score;
}
}
}
}
int main()
{
int n, m;
int num, score;
list_t list1, list2;
init_list(&list1);
init_list(&list2);
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%d %d", &num, &score);
tail_insert(&list1, num, score);
}
for (int i = 0; i < m; i++)
{
scanf("%d %d", &num, &score);
tail_insert(&list2, num, score);
}
//print(&list1);
//print(&list2);
link_list(&list1, &list2);
//print(&list1);
list_sort(&list1);
print(&list1);
return 0;
}
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复