#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;
}
Ddddddd_158
- 粉丝: 3163
- 资源: 729
最新资源
- AllSort(直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序)
- 模拟qsort,改造冒泡排序使其能排序任意数据类型,即日常练习
- 数组经典习题之顺序排序和二分查找和冒泡排序
- 基于 Oops Framework 提供的游戏项目开发模板,项目中提供了最新版本 Cocos Creator 3.x 插件与游戏资源初始化通用逻辑
- live-ai这是一个深度学习的资料
- FeiQ.rar 局域网内通信服务软件
- 172.16.100.195
- 光储并网simulink仿真模型,直流微电网 光伏系统采用扰动观察法是实现mppt控制,储能可由单独蓄电池构成,也可由蓄电池和超级电容构成的混合储能系统,并采用lpf进行功率分配 并网采用pq控制
- python编写微信读取smart200plc的数据发送给微信联系人
- 光储并网VSG系统Matlab simulink仿真模型,附参考文献 系统前级直流部分包括光伏阵列、变器、储能系统和双向dcdc变器,后级交流子系统包括逆变器LC滤波器,交流负载 光储并网VSG系
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈