解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> //快速排序 void quickSort(int *a, int low, int high) { int i = low; int j = high; int key = a[low]; if (low >= high) { return; } while (low < high) { while (low < high && a[high] >= key) { high--; } if (a[high] < key) { a[low] = a[high]; low++; } while (low < high && a[low] <= key) { low++; } if (a[low] > key) { a[high] = a[low]; high--; } } a[low] = key; quickSort(a, i, low); quickSort(a, low + 1, j); } void operate(int *shortArray, int sizeOfShort, int *longArray, int sizeOfLong, int *newArray) { //合并两个集合为一个集合 for (int i = 0; i < sizeOfShort; ++i) { newArray[i] = shortArray[i]; } int j = 0; int i = 0; for (i = sizeOfShort; j < sizeOfLong; ++i, ++j) { newArray[i] = longArray[j]; } //升序排序 quickSort(newArray, 0, i - 1); //开始去掉重复元素 for (int k = 0; k < sizeOfShort + sizeOfLong - 1; ++k) { for (int l = k + 1; l < sizeOfLong + sizeOfShort; ++l) { if ((newArray[k] == newArray[l]) && newArray[k] != -1 && newArray[l] != -1) { newArray[l] = -1;//将位置靠后的数字置为-1从而模拟删除重复的元素 } } } } int main() { int n = 0, m = 0; while (scanf("%d %d", &n, &m) == 2) { int arrayOfN[n], arrayOfM[m]; for (int i = 0; i < n; ++i) { scanf("%d", &arrayOfN[i]); } for (int i = 0; i < m; ++i) { scanf("%d", &arrayOfM[i]); } //合并两个集合并去掉重复的元素 int newArray[n + m];//新数组容量为两个数组元素之和,可能用不了 operate(arrayOfN, n, arrayOfM, m, newArray); //输出结果 for (int i = 0; i < n + m; ++i) { if (newArray[i] != -1) { printf("%d ", newArray[i]); } } printf("\n"); } return 0; }
0.0分
4 人评分