使用数组模拟有序表的并运算,放入c中。 void sbing(int a[],int b[],int c[]) { } void main() { int a[10]={1,3,5,7,8}; int b[10]={2,4,6,8,9}; int c[10]={0}; sbing(a,b,c); }
时间: 2023-05-31 09:03:18 浏览: 63
void sbing(int a[], int b[], int c[]) {
int i = 0, j = 0, k = 0;
while (i < 5 && j < 5) {
if (a[i] < b[j])
c[k++] = a[i++];
else if (a[i] > b[j])
c[k++] = b[j++];
else { // 如果两个数组当前位置的值相等,则只取其中一个值放入c中
c[k++] = a[i++];
j++;
}
}
// 将剩余的元素放入c中
while (i < 5)
c[k++] = a[i++];
while (j < 5)
c[k++] = b[j++];
}
int main() {
int a[10] = {1, 3, 5, 7, 8};
int b[10] = {2, 4, 6, 8, 9};
int c[10] = {0};
sbing(a, b, c);
for (int i = 0; i < 9; i++)
printf("%d ", c[i]);
printf("%d\n", c[9]);
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)