题解 | #有序序列合并#
有序序列合并
https://www.nowcoder.com/practice/a9e943b0dab142759807d0cfb6863897
#include <stdio.h> int main() { int n = 0; int m = 0; int arr1[1000] = { 0 }; int arr2[1000] = { 0 }; scanf("%d %d", &n, &m); int i = 0; int j = 0; int tmp = 0; for (i = 0; i < n; i++) { scanf("%d", &arr1[i]);//给第一个数组赋值 } for (i = 0; i < m; i++) { scanf("%d", &arr2[i]);//给第二个数组赋值 } int arr[2000] = { 0 };//合并数组 for (i = 0; i < n + m; i++) { if (i < n) { //i小于n时,给合并数组传arr1对应的值 arr[i] = arr1[i]; } else { arr[i] = arr2[i-n]; //i大于等于n且小于n+m时,给合并数组传arr2对应坐标的值 } } //冒泡排序 //共n+m个元素 //要比n+m-1轮 //每轮n+m-1-i次 for (i = 0; i < n + m - 1; i++) { for (j = 0; j < n + m - 1 - i; j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } for (i = 0; i < n + m; i++) { printf("%d ", arr[i]); } return 0; }