原题链接:[编程入门]链表合并
合并链表之后再排序
原本的思路是将两个有序的链表来回引用指针排序
但是不会
注释的语句是有序链表合并的源代码
#include<iostream>
using namespace std;
struct a {
int number;
int achievement;
};
struct b {
int number;
int achievement;
};
struct c {
int number;
int achievement;
};
int main()
{
int n_int;
int m_int;
cin >> n_int;
cin >> m_int;
a* p = new a[n_int];
for (int i_for1_int = 0; i_for1_int < n_int; i_for1_int++) {
cin >> (*(p + i_for1_int)).number;
cin >> (*(p + i_for1_int)).achievement;
}
b* q = new b[m_int];
for (int i_for1_int = 0; i_for1_int < m_int; i_for1_int++) {
cin >> (*(q + i_for1_int)).number;
cin >> (*(q + i_for1_int)).achievement;
}
/*for (int i_for1_int = 0; i_for1_int < (n_int>m_int ? n_int :m_int); i_for1_int++) {
if (i_for1_int < n_int) {
for (int i_for2_int = 0; i_for2_int < n_int; i_for2_int++) {
if ((*(p + i_for1_int)).number > (*(p + i_for2_int)).number) {
int change_int=(*(p + i_for1_int)).number;
(*(p + i_for1_int)).number = (*(p + i_for2_int)).number;
(*(p + i_for2_int)).number = change_int;
change_int = (*(p + i_for1_int)).achievement;
(*(p + i_for1_int)).achievement = (*(p + i_for2_int)).achievement;
(*(p + i_for2_int)).achievement = change_int;
}
}
}
if (i_for1_int < m_int) {
for (int i_for2_int = 0; i_for2_int < m_int; i_for2_int++) {
if ((*(q + i_for1_int)).number > (*(q + i_for2_int)).number) {
int change_int = (*(q + i_for1_int)).number;
(*(q + i_for1_int)).number = (*(q + i_for2_int)).number;
(*(q + i_for2_int)).number = change_int;
change_int = (*(q + i_for1_int)).achievement;
(*(q + i_for1_int)).achievement = (*(q + i_for2_int)).achievement;
(*(q + i_for2_int)).achievement = change_int;
}
}
}
}*/
c* v = new c[m_int + n_int];
for (int i_for1_int = 0; i_for1_int < n_int+m_int; i_for1_int++) {
if (i_for1_int < n_int) {
(*(v + i_for1_int)).number = (*(p + i_for1_int)).number;
(*(v + i_for1_int)).achievement = (*(p + i_for1_int)).achievement;
}else{
(*(v + i_for1_int)).number = (*(q + i_for1_int-n_int)).number;
(*(v + i_for1_int)).achievement = (*(q + i_for1_int-n_int)).achievement;
}
}
for (int i_for1_int = 0; i_for1_int < n_int + m_int; i_for1_int++) {
for (int i_for2_int = 0; i_for2_int < n_int + m_int; i_for2_int++) {
if ((*(v + i_for1_int)).number < (*(v + i_for2_int)).number) {
int change_int = (*(v + i_for1_int)).number;
(*(v + i_for1_int)).number = (*(v + i_for2_int)).number;
(*(v + i_for2_int)).number = change_int;
change_int = (*(v + i_for1_int)).achievement;
(*(v + i_for1_int)).achievement = (*(v + i_for2_int)).achievement;
(*(v + i_for2_int)).achievement = change_int;
}
}
}
/*for (int i_for1_int = 0; i_for1_int < m_int+n_int; i_for1_int++) {
for (int i_for2_int = 0; i_for2_int < n_int; i_for2_int++) {
for (int i_for3_int = 0; i_for3_int < m_int; i_for3_int++) {
if ((*(p + i_for2_int)).number < (*(q + i_for3_int)).number) {
(*(v + i_for1_int)).number = (*(p + i_for2_int)).number;
(*(v + i_for1_int)).achievement = (*(p + i_for2_int)).achievement;
}
else {
(*(v + i_for1_int)).number = (*(q + i_for3_int)).number;
(*(v + i_for1_int)).achievement = (*(q + i_for3_int)).achievement;
}
}
}
}*/
for (int i_for1_int = 0; i_for1_int < n_int+m_int; i_for1_int++) {
cout << (*(v + i_for1_int)).number << " " << (*(v + i_for1_int)).achievement << endl;
}
}
0 分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复