已解决
王道p18 第11题 现在有两个等长升序序列 A和 B,试设计一个在时间和空间两方面都尽可能高效的算法,找出两个序列 A和B的中位数。
来自网友在路上 195895提问 提问时间:2023-11-07 18:54:58阅读次数: 95
最佳答案 问答题库958位专家为你答疑解惑
视频讲解👇:
p18 第10题 c语言代码实现王道数据结构课后代码题_哔哩哔哩_bilibili
本题代码如下
int search(int a[], int b[], int c[])
{int i = 0;int j = 0;int k = 0;while (i < 5 && j < 5){if (a[i] <= b[j])c[k++] = a[i++];elsec[k++] = b[j++];}while (i < 5)c[k++] = a[i++];while (j < 5)c[k++] = b[j++];return c[(k - 1) / 2];
}
完整测试代码
#include<stdio.h>
int a[5] = { 11,13,15,17,19 };
int b[5] = { 2,4,6,8,20 };
int n = 5;
int c[10];
int search(int a[], int b[], int c[])
{int i = 0;int j = 0;int k = 0;while (i < 5 && j < 5){if (a[i] <= b[j])c[k++] = a[i++];elsec[k++] = b[j++];}while (i < 5)c[k++] = a[i++];while (j < 5)c[k++] = b[j++];return c[(k - 1) / 2];
}
int main()
{int ret = search(a, b, c);for (int i = 0; i < 10; i++)printf("%d ", c[i]);printf("中位数为:%d", ret);return 0;
}
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"王道p18 第11题 现在有两个等长升序序列 A和 B,试设计一个在时间和空间两方面都尽可能高效的算法,找出两个序列 A和B的中位数。":http://eshow365.cn/6-34668-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!