-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.c
More file actions
58 lines (54 loc) · 1.27 KB
/
04.c
File metadata and controls
58 lines (54 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
int ai = 0, bj = 0, ck = 0, length = 0;
int *res = NULL;
double result;
length = nums1Size + nums2Size;
res = (int *)malloc(length*sizeof(int));
for (ck=0; ck< length; ck++){
if(ai == nums1Size)
{
res[ck] = nums2[bj];
bj++;
}
else if(bj == nums2Size)
{
res[ck] = nums1[ai];
ai++;
}
else
{
if(nums1[ai] < nums2[bj])
{
res[ck] = nums1[ai];
ai++;
}
else
{
res[ck] = nums2[bj];
bj++;
}
}
}
for (ck = 0; ck < length;ck++)
{
printf("%d ", res[ck]);
}
if (length % 2 == 0)
result = (res[length / 2 - 1] + res[length / 2]) / 2.0;
else
result = res[length / 2];
return result;
}
int main()
{
int nums1[2] = {1, 3};
int nums2[2] = {2, 4};
double i = 0;
//int *c = NULL;
//c = findMedianSortedArrays(nums1, 4, nums2, 4);
i = findMedianSortedArrays(nums1, 2, nums2, 2);
printf("%lf ", i);
return 0;
}