|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include"stdio.h"
#define m 5
#define n 4
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
{
int i, j, t, a[nums1Size+nums2Size];
double mid_digit;
for(i = 0, j = 0; i < nums1Size; j++, i++)
{
a[i] = nums1[j];
}
for(i = nums1Size, j = 0; i < nums1Size + nums2Size; i++)
{
a[i] = nums2[j];
j++;
}
for(i = 0; i < (nums1Size+ nums2Size) - 1; i++)
{
for(j = i + 1; j < nums1Size + nums2Size; j++)
{
if(a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
if((nums1Size + nums2Size) % 2 != 0)
{
return mid_digit = 1.0 * a[(nums1Size + nums2Size) / 2];
}
else
{
mid_digit = (a[(nums1Size + nums2Size - 1) / 2] + a[(nums1Size + nums2Size + 1) / 2]) / 2;
return mid_digit;
}
int main()
{
int m, n, i, j;
double mid_digit;
//printf("Please input the size of array nums1 and nums2:");
//scanf("%d%d", &m, &n);
int nums1[m], nums2[n];
printf("Please input some numbers:\n");
for(i = 0; i < m; i++)
{
scanf("%d", nums1[i]);
}
for(j = 0; j < n; j++)
{
scanf("%d", nums2[j]);
}
mid_digit = findMedianSortedArrays(nums1, m, nums2, n);
printf("The middle digit is %.1lf\n", mid_digit);
return 0;
}
}
报错2 13 C:\c程序\JOB\寻找两个有序数组的中位数.c [Error] expected identifier or '(' before numeric constant
#define m 5
#define n 4
int m, n, i, j;
double mid_digit;
//printf("Please input the size of array nums1 and nums2:");
//scanf("%d%d", &m, &n);
int nums1[m], nums2[n];
我觉得这里的m,n并没有使用你宏定义的5和4,而是主函数里 int m,n,把这句删了试试看
|
|