输入包含三行,
第一行包含两个正整数n, m(1 ≤ n,m ≤ 100),用空格分隔。n表示第二行第一个升序序列中数字的个数,m表示第三行第二个升序序列中数字的个数。
第二行包含n个整数(范围1~5000),用空格分隔。
第三行包含m个整数(范围1~5000),用空格分隔。
输出为一行,输出长度为n+m的升序序列,即长度为n的升序序列和长度为m的升序序列中的元素重新进行升序序列排列合并。
5 6 1 3 7 9 22 2 8 10 17 33 44
1 2 3 7 8 9 10 17 22 33 44
// // Created by yuanhao on 2019-9-16. // #include <iostream> using namespace std; int main() { int n; int m; cin >> n >> m; int a[n]; int b[m]; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < m; ++i) { cin >> b[i]; } int len = n + m; int index = 0; int ai = 0; int bi = 0; while (index < len) { if (ai == n) { cout << b[bi] << " "; bi++; } else if (bi == m) { cout << a[ai] << " "; ai++; } else if (a[ai] > b[bi]) { cout << b[bi] << " "; bi++; } else { cout << a[ai] << " "; ai++; } index++; } cout << endl; return 0; }
#define SIZE 5000 #include <stdio.h> int main() { int arr_n[SIZE] = {0}; int arr_m[SIZE] = {0}; int n = 0; int m = 0; int i = 0; int j = 0; //输入n、m scanf("%d %d", &n, &m); //输入n数列 for (i = 0; i < n; i++) { scanf("%d", arr_n + i); } //输入m数列 for (i = 0; i < m; i++) { scanf("%d", arr_m + i); } i = 0; //进行比较打印 while (i < n && j < m) { if (arr_n[i] < arr_m[j]) { printf("%d ", arr_n[i]); i++; } else { printf("%d ", arr_m[j]); j++; } } //打印剩余数组 while (i < n) { printf("%d ", arr_n[i]); i++; } while (j < m) { printf("%d ", arr_m[j]); j++; } return 0; }
#include<bits/stdc++.h> using namespace std; int n,m,a[10001]; int main(){ cin>>n>>m; for(int i=0;i<n+m;++i) cin>>a[i]; sort(a,a+n+m); for(int i=0;i<n+m;++i) cout<<a[i]<<" "; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <climits> using namespace std; const int N = 110; int n, m; int a[N], b[N]; int main() { cin.tie(); ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; int i = 0, j = 0; while (i < n && j < m) { if (a[i] < b[j]) { cout << a[i++] << " "; } else { cout << b[j++] << " "; } } while (i < n) cout << a[i++] << " "; while (j < m) cout << b[j++] << " "; cout << endl; return 0; }
let inputNum = readline().split(' '); let N = parseInt(inputNum[0]); let M = parseInt(inputNum[1]); let listN = readline().split(' '); let listM = readline().split(' '); let newList = []; for(let i=0; i<N; i++){ while(listM.length && parseInt(listN[i])>parseInt(listM[0])){ newList.push(listM.shift()); } newList.push(listN[i]); } while(listM.length){ newList.push(listM.shift()); } print(newList.join(' '));