#include <stdio.h>
#include <stdlib.h>
static double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
{
int sum = nums1Size + nums2Size;
int *nums = malloc(sizeof(int) * sum);
int i = 0, j = 0, k = 0;
int half = sum / 2 + 1;
while (k < half) {
int n;
if (i < nums1Size && j < nums2Size) {
n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
} else if (i < nums1Size) {
n = nums1[i++];
} else if (j < nums2Size) {
n = nums2[j++];
}
nums[k++] = n;
}
if (sum % 2) {
return nums[k-1];
} else {
return (nums[k-1] + nums[k-2]) / 2.0;
}
}
int main(int argc, char **argv)
{
int r1[] = {1};
int r2[] = {2};
int n1 = sizeof(r1)/sizeof(r1[0]);
int n2 = sizeof(r2)/sizeof(r2[0]);
printf("Median is 1.5 = %f\n", findMedianSortedArrays(r1, n1, r2, n2));
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45, 50};
n1 = sizeof(ar1)/sizeof(ar1[0]);
n2 = sizeof(ar2)/sizeof(ar2[0]);
printf("Median is 17 = %f\n", findMedianSortedArrays(ar1, n1, ar2, n2));
int ar11[] = {1, 12, 15, 26, 38};
int ar21[] = {2, 13, 17, 30, 45 };
n1 = sizeof(ar11)/sizeof(ar11[0]);
n2 = sizeof(ar21)/sizeof(ar21[0]);
printf("Median is 16 = %f\n", findMedianSortedArrays(ar11, n1, ar21, n2));
int a1[] = {1, 2, 5, 6, 8 };
int a2[] = {13, 17, 30, 45, 50};
n1 = sizeof(a1)/sizeof(a1[0]);
n2 = sizeof(a2)/sizeof(a2[0]);
printf("Median is 10.5 = %f\n", findMedianSortedArrays(a1, n1, a2, n2));
int a10[] = {1, 2, 5, 6, 8, 9, 10 };
int a20[] = {13, 17, 30, 45, 50};
n1 = sizeof(a10)/sizeof(a10[0]);
n2 = sizeof(a20)/sizeof(a20[0]);
printf("Median is 9.5 = %f\n", findMedianSortedArrays(a10, n1, a20, n2));
int a11[] = {1, 2, 5, 6, 8, 9 };
int a21[] = {13, 17, 30, 45, 50};
n1 = sizeof(a11)/sizeof(a11[0]);
n2 = sizeof(a21)/sizeof(a21[0]);
printf("Median is 9 = %f\n", findMedianSortedArrays(a11, n1, a21, n2));
int a12[] = {1, 2, 5, 6, 8 };
int a22[] = {11, 13, 17, 30, 45, 50};
return 0;
}
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
Ddddddd_158
- 粉丝: 2557
- 资源: 591
最新资源
- system_file1711460535847
- 前端分析-202307110078
- 前端分析-202307110078
- 西门子S7-1200控制5轴伺服程序加维纶触摸屏画面案例 1.PTO伺服轴脉冲定位控制功能应用+速度模式应用+扭矩模式应用
- 案例程序5 伺服精准同步程序,西门子200 PLC与信捷DS2伺服通讯程序,可以实现同步运动 将编码器直接连接到伺服驱动器上
- jdk17安装包Windows x64
- ssm+mysql的学生选课管理系统(源码+lw+ppt)
- C#上位机与omron欧姆龙 Fins TCP通信以太网通讯实例 源码 通过和PLC用网络连接,可以读取写入欧姆龙PLC的数据寄
- VASP最新PBE赝势库(2023.11)
- java基于ssm+jsp课程思政元素收集遴选系统源码 带毕业论文+PPT
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)