题解 | #有序序列合并#
有序序列合并
https://www.nowcoder.com/practice/a9e943b0dab142759807d0cfb6863897
#include <stdio.h> int main() { //得到n,m两个数 int m, n; scanf("%d %d\n", &n, &m); //扫描第一个序列数组 int sequencearray1[1000]; for (int i = 1; i <= n; i++) { int index = 0; scanf("%d", &index); sequencearray1[i] = index; } //扫描第二个序列数组 int sequencearray2[1000]; for (int j = 1; j <= m; j++) { int index = 0; scanf("%d", &index); sequencearray2[j] = index; } //合并数组 int merge_sequencearrays[2000]; int sumnumbers = m + n; for (int k = 1; k <= sumnumbers; k++) { if (k <= n) { merge_sequencearrays[k] = sequencearray1[k]; } else { merge_sequencearrays[k] = sequencearray2[k - n]; } } //对数组进行排序; int aftercompare[2000]; for (int l = 1; l <= sumnumbers; l++) { int max_num = 0; for (int b = 1; b <= sumnumbers; b++) { if (merge_sequencearrays[b] > max_num) { max_num = merge_sequencearrays[b]; } } aftercompare[l] = max_num; //// 移除已选的最大值,避免下次循环重复选择 for (int k = 1; k <= sumnumbers; k++) { if (merge_sequencearrays[k] == max_num) { merge_sequencearrays[k] = -1; // 用一个标志值替代已选的最大值 break; } } } //输出排序后的数组(这里题目看错了,把最大值放在了第一位,没办法,懒得重新写,反向输出得了) for (int d=sumnumbers; d>=1;d--) { printf("%d ",aftercompare[d]); } return 0; }