p18-7
#include <stdio.h>
int main() {
int a[5] = {0, 2, 4, 6, 8};
int b[5] = {1, 3, 5, 5, 5};
int c[10];
int i = 0, j = 0, k=0;
while (i < 5 && j < 5) {
if (a[i] < b[j]) {
c[k++] = a[i++];
} else {
c[k++] = b[j++];
}
}
while (i < 5) {
c[k++] = a[i++];
}
while (j < 5) {
c[k++] = b[j++];
}
for (int n = 0; n < 10; n++) {
printf("%d\n", c[n]);
}
}
#include <stdio.h>
int main() {
int a[5] = {0, 2, 6, 6, 10};
int b[6] = {1, 3, 5, 7, 8, 9};
int c[11];
int i = 0, j = 0, pos;
for (pos = 0; pos < 11; pos++) {
c[pos] = a[i] < b[j] ? a[i++] : b[j++];
}
for (i = 0; i < pos; i++) {
printf("%d ", c[i]);
}
}
# 这种方法数组超出了边界,是错误的!切记。