题解 | #有序序列合并#
有序序列合并
https://www.nowcoder.com/practice/a9e943b0dab142759807d0cfb6863897
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int a1[n];//读取第一个数组 for (int i = 0; i < n; i++) { scanf("%d", &a1[i]); } int a2[m];//读取第二个数组 for (int i = 0; i < m; i++) { scanf("%d", &a2[i]); } //比较并打印,tag就是下标,tag_a就是数组a1的下标 for (int i = 0, tag_a1 = 0, tag_a2 = 0; i < m + n; i++) { if (a1[tag_a1] <= a2[tag_a2]) { printf("%d ", a1[tag_a1]); tag_a1++; } else { printf("%d ", a2[tag_a2]); tag_a2++; } if (tag_a1 == n ) a1[tag_a1] = 30001; if (tag_a2 == m ) a2[tag_a2] = 30001; } return 0; }