有两个升序的数组,合成一个非降序的数组
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
int a[] = {1, 5, 7, 9, 12, 15, 19};
int b[] = {2, 3, 6, 8, 9};
int aCount = sizeof(a) / sizeof(a[0]);
int bCount = sizeof(b) / sizeof(b[0]);
int c[aCount + bCount];
int aPoint = 0;
int bPoint = 0;
for (int i = 0; i < aCount + bCount; i++) {
if (aPoint > aCount - 1) {
c[i] = b[bPoint];
bPoint++;
} else if (bPoint > bCount - 1) {
c[i] = a[aPoint];
aPoint++;
} else if (aPoint < aCount && a[aPoint] < b[bPoint]) {
c[i] = a[aPoint];
aPoint++;
} else {
c[i] = b[bPoint];
bPoint++;
}
}
for (int i = 0; i < sizeof(c) / sizeof(c[0]); i++) {
printf("%d\t", c[i]);
}
return 0;
}