我尝试在C ++中实现上述算法,但是在计算数组的一半长度时,我得到一个舍入错误。
尝试:
#include <iostream>
#include <math.h>
using namespace std;
void print_array(int arr[], int size){
for(int i = 0; i < size; i++){
cout << arr[i] << "-";
}
cout << endl;
}
float median_h(int arr[], int size){
float median = 0.0;
if(size%2 == 0)
median = (arr[size/2] + arr[(size/2)-1])/2;
else median = arr[(int)ceil(size/2)];
return median;
}
float median(int arr1[], int arr2[], int size_1, int size_2){
cout << size_1 << endl;
print_array(arr1,size_1);
cout << size_2 << endl;
print_array(arr2,size_2);
cout << endl;
float m1 = median_h(arr1,size_1);
float m2 = median_h(arr2,size_2);
float median_res = 0.0;
if(size_1 == 1)
median_res = (arr1[0] + arr2[0])/2;
else if(m1 == m2)
median_res = m1;
else{
int index = 0;
int size = ceil(size_1/2);
(size_1 % 2 == 0)? index = size_1/2 : index = floor(size_1/2);
if(m1 < m2)
median_res = median(arr1 + index,arr2, size, size);
else
median_res = median(arr1,arr2 + index, size, size);
}
return median_res;
}
int main(void){
int arr1[] = {1,12,15,26,38};
int arr2[] = {2,13,17,30,45};
float med = median(arr1,arr2,5,5);
cout << med << endl;
}
这是输出:
output:
5
1-12-15-26-38-
5
2-13-17-30-45-
2
15-26-
2
2-13-
1
15-
1
13-
14
我希望在第二次递归迭代中长度为3,但我得到2.我不知道ceil(5/2)的错误。它应该是3。
答案 0 :(得分:3)
int size = ceil(size_1/2);
由于size_1的类型为int,因此将其除以2隐含等效于取得除法的最低限度。例如如果size_1等于3,则(size_1 / 2)将返回1,而不是1.5。
因此,ceil()不会在这里得到你想要的东西,因为传递给它的值已经被覆盖了。
你可以这样做:
int size = ceil(size_1/2.0f); // now we're dividing by a float, so the result will be non-integer
...但你可以得到相同的结果,而不必仅仅通过在分割前将值加1来求助于浮点数学:
int size = (size_1+1)/2;
答案 1 :(得分:1)
/* Function to get median of a sorted array */
int median_h(int arr[], int n)
{
if (n%2 == 0)
return (arr[n/2] + arr[n/2-1])/2;
else
return arr[n/2]; // <- HERE
// also since n is int, you can do ( n << 1 )
// it's faster
}
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int median(int ar1[], int ar2[], int n)
{
/* return -1 for invalid input */
if (n <= 0)
return -1;
if (n == 1)
return (ar1[0] + ar2[0])/2;
if (n == 2)
return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2;
int m1 = median_h(ar1, n); /* get the median of the first array */
int m2 = median_h(ar2, n); /* get the median of the second array */
/* If medians are equal then return either m1 or m2 */
if (m1 == m2)
return m1;
/* if m1 < m2 then median must exist in ar1[m1....] and
ar2[....m2] */
if (m1 < m2)
{
if (n % 2 == 0)
return median(ar1 + n/2 - 1, ar2, n - n/2 +1);
return median(ar1 + n/2, ar2, n - n/2);
}
/* if m1 > m2 then median must exist in ar1[....m1] and
ar2[m2...] */
if (n % 2 == 0)
return median(ar2 + n/2 - 1, ar1, n - n/2 + 1);
return median(ar2 + n/2, ar1, n - n/2);
}
使用本网站: https://www.geeksforgeeks.org/median-of-two-sorted-arrays/