#include #include using namespace std;

int main() { int n, m; cin >> n >> m; vector t(n); vector d(m); for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < m; i++) { cin >> d[i]; }

int i = 0, j = 0;
while (i < n && j < m) {
    if (t[i] <= d[j]) {
        cout << t[i] << ' '; // 输出原始数组中的元素
        i++;
    } else {
        cout << d[j] << ' '; // 输出新插入的元素
        j++;
    }
}

// 输出剩余的原始数组元素
while (i < n) {
    cout << t[i] << ' ';
    i++;
}

// 输出剩余的新插入元素
while (j < m) {
    cout << d[j] << ' ';
    j++;
}

return 0;

}

C++ 编程题:题库插入算法 - 如何在排序数组中插入新元素

原文地址: https://www.cveoy.top/t/topic/qmlV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录