合并链表之后再排序
原本的思路是将两个有序的链表来回引用指针排序
但是不会
注释的语句是有序链表合并的源代码

  1. #include<iostream>
  2. using namespace std;
  3. struct a {
  4. int number;
  5. int achievement;
  6. };
  7. struct b {
  8. int number;
  9. int achievement;
  10. };
  11. struct c {
  12. int number;
  13. int achievement;
  14. };
  15. int main()
  16. {
  17. int n_int;
  18. int m_int;
  19. cin >> n_int;
  20. cin >> m_int;
  21. a* p = new a[n_int];
  22. for (int i_for1_int = 0; i_for1_int < n_int; i_for1_int++) {
  23. cin >> (*(p + i_for1_int)).number;
  24. cin >> (*(p + i_for1_int)).achievement;
  25. }
  26. b* q = new b[m_int];
  27. for (int i_for1_int = 0; i_for1_int < m_int; i_for1_int++) {
  28. cin >> (*(q + i_for1_int)).number;
  29. cin >> (*(q + i_for1_int)).achievement;
  30. }
  31. /*for (int i_for1_int = 0; i_for1_int < (n_int>m_int ? n_int :m_int); i_for1_int++) {
  32. if (i_for1_int < n_int) {
  33. for (int i_for2_int = 0; i_for2_int < n_int; i_for2_int++) {
  34. if ((*(p + i_for1_int)).number > (*(p + i_for2_int)).number) {
  35. int change_int=(*(p + i_for1_int)).number;
  36. (*(p + i_for1_int)).number = (*(p + i_for2_int)).number;
  37. (*(p + i_for2_int)).number = change_int;
  38. change_int = (*(p + i_for1_int)).achievement;
  39. (*(p + i_for1_int)).achievement = (*(p + i_for2_int)).achievement;
  40. (*(p + i_for2_int)).achievement = change_int;
  41. }
  42. }
  43. }
  44. if (i_for1_int < m_int) {
  45. for (int i_for2_int = 0; i_for2_int < m_int; i_for2_int++) {
  46. if ((*(q + i_for1_int)).number > (*(q + i_for2_int)).number) {
  47. int change_int = (*(q + i_for1_int)).number;
  48. (*(q + i_for1_int)).number = (*(q + i_for2_int)).number;
  49. (*(q + i_for2_int)).number = change_int;
  50. change_int = (*(q + i_for1_int)).achievement;
  51. (*(q + i_for1_int)).achievement = (*(q + i_for2_int)).achievement;
  52. (*(q + i_for2_int)).achievement = change_int;
  53. }
  54. }
  55. }
  56. }*/
  57. c* v = new c[m_int + n_int];
  58. for (int i_for1_int = 0; i_for1_int < n_int+m_int; i_for1_int++) {
  59. if (i_for1_int < n_int) {
  60. (*(v + i_for1_int)).number = (*(p + i_for1_int)).number;
  61. (*(v + i_for1_int)).achievement = (*(p + i_for1_int)).achievement;
  62. }else{
  63. (*(v + i_for1_int)).number = (*(q + i_for1_int-n_int)).number;
  64. (*(v + i_for1_int)).achievement = (*(q + i_for1_int-n_int)).achievement;
  65. }
  66. }
  67. for (int i_for1_int = 0; i_for1_int < n_int + m_int; i_for1_int++) {
  68. for (int i_for2_int = 0; i_for2_int < n_int + m_int; i_for2_int++) {
  69. if ((*(v + i_for1_int)).number < (*(v + i_for2_int)).number) {
  70. int change_int = (*(v + i_for1_int)).number;
  71. (*(v + i_for1_int)).number = (*(v + i_for2_int)).number;
  72. (*(v + i_for2_int)).number = change_int;
  73. change_int = (*(v + i_for1_int)).achievement;
  74. (*(v + i_for1_int)).achievement = (*(v + i_for2_int)).achievement;
  75. (*(v + i_for2_int)).achievement = change_int;
  76. }
  77. }
  78. }
  79. /*for (int i_for1_int = 0; i_for1_int < m_int+n_int; i_for1_int++) {
  80. for (int i_for2_int = 0; i_for2_int < n_int; i_for2_int++) {
  81. for (int i_for3_int = 0; i_for3_int < m_int; i_for3_int++) {
  82. if ((*(p + i_for2_int)).number < (*(q + i_for3_int)).number) {
  83. (*(v + i_for1_int)).number = (*(p + i_for2_int)).number;
  84. (*(v + i_for1_int)).achievement = (*(p + i_for2_int)).achievement;
  85. }
  86. else {
  87. (*(v + i_for1_int)).number = (*(q + i_for3_int)).number;
  88. (*(v + i_for1_int)).achievement = (*(q + i_for3_int)).achievement;
  89. }
  90. }
  91. }
  92. }*/
  93. for (int i_for1_int = 0; i_for1_int < n_int+m_int; i_for1_int++) {
  94. cout << (*(v + i_for1_int)).number << " " << (*(v + i_for1_int)).achievement << endl;
  95. }
  96. }
点赞(0)
 

0 分

0 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论