将两个有序数组合并为一个有序数组,重复元素保留一份。
#include <stdio.h>
#include <stdlib.h>
int arry2one(int *arr1, int arr1size, int *arr2, int arr2size)
{
int arr1min = 0;
int arr2min = 0;
int arrallmin = 0;
int *arrall = NULL;
int k = 0;
arrall = malloc(sizeof(int) * (arr1size+arr2size));
if(!arrall)
{
return -1;
}
while(arrallmin < (arr1size + arr2size -1) && arr1min < arr1size && arr2min < arr2size)
{
if(arr1[arr1min] <= arr2[arr2min])
{
arrall[arrallmin] = arr1[arr1min];
arr1min++;
}
else
{
arrall[arrallmin] = arr2[arr2min];
arr2min++;
}
arrallmin++;
}
while(k < (arr1size + arr2size -1))
{
printf("%d\n", arrall[k]);
k++;
}
}
int main(int argc, char **argv)
{
int a[5] = {1,3,5,7,9};
int b[5] = {2,4,6,8,10};
arry2one(a,5,b,5);
}