Why my code can not run???? it works well in my local DEV C++......


  • 0
    A

    Why my code can not run???? it works well in my local DEV C++......For the input[1,2],[3,4], the anwer is always 2.0000, but the output is 2.5000 in local......why.....

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    float max1,max2;
    scanf("%d%d",&nums1Size,&nums2Size);
    int k;
    if(nums1Size>nums2Size)
    {
    for(k=0;k<nums1Size;k++)
    scanf("%d",&nums1[k]);
    for(k=0;k<nums2Size;k++)
    scanf("%d",&nums2[k]);
    }
    else
    {
    k=nums1Size;
    nums1Size=nums2Size;
    nums2Size=k;
    for(k=0;k<nums2Size;k++)
    scanf("%d",&nums2[k]);
    for(k=0;k<nums1Size;k++)
    scanf("%d",&nums1[k]);
    }
    int i,j,start=0,end=nums2Size;

       while(start<=end)
    {
    	i=(start+end)/2;
    	j=(nums1Size+nums2Size)/2-i;
      
        if(nums2[i]<nums1[j-1]&&i<nums2Size)
        start=i+1;
    	else if(nums2[i-1]>nums1[j]&&i>0)
        end=i-1;  
    	else
    	{
    		if(i==nums2Size)
    		{
    			max2=nums1[(nums1Size-nums2Size)/2];
    		}
    		else if(j==nums1Size)
    		{
    			max2=nums1[j-1];
    
    		}
    		else 
    		{
    			max2=nums2[i]<nums1[j]?nums2[i]:nums1[j];
    		}
    		if((nums1Size+nums2Size)%2==0)
    		{
    			if(j==nums1Size)
    			max1=nums1[j-1];
    			else if(i==nums2Size)
    			max1=nums2[i-1];
    			else max1=nums2[i-1]<nums1[j-1]?nums1[j-1]:nums2[i-1];
    			
    			
    		}
    	
    		break;
    	}
    }
    if((nums1Size+nums2Size)%2==0)
    return (max1+max2)/2;
    else
    return max2;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.