题解 | #有序序列合并#

有序序列合并

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;
}

全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务