#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
- 粉丝: 3162
- 资源: 729
最新资源
- C语言-leetcode题解之61-rotate-list.c
- C语言-leetcode题解之59-spiral-matrix-ii.c
- C语言-leetcode题解之58-length-of-last-word.c
- 计算机编程课程设计基础教程
- (源码)基于C语言的系统服务框架.zip
- (源码)基于Spring MVC和MyBatis的选课管理系统.zip
- (源码)基于ArcEngine的GIS数据处理系统.zip
- (源码)基于JavaFX和MySQL的医院挂号管理系统.zip
- (源码)基于IdentityServer4和Finbuckle.MultiTenant的多租户身份认证系统.zip
- (源码)基于Spring Boot和Vue3+ElementPlus的后台管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈