1 条题解

  • 0
    @ 2025-2-21 19:49:05

    C :

    /*设a[0:n-1]是一个已排好序的数组。请改写二分搜索算法,使得当搜索元素x不在数组中时,
    返回小于x的最大元素的位置 i 和大于x的最小元素位置 j。当搜索元素在数组中时,i 和 j 相同,
    均为x在数组中的位置。*/
    #include<stdio.h>
    int main()
    {
    	void BFind(int *,int ,int,int);
    	int x,n,i;
    	scanf("%d%d",&x,&n);
    	int t[n];
    	for(i=0;i<n;i++)
            scanf("%d",&t[i]);
    	BFind(t,0,n,x);
    	return 0;
    }
    void BFind(int *t,int m,int n,int x)
    {
    	while(m<n)
    	{
    		if(x>t[(m+n)/2])
    			m=(m+n)/2+1;
            if(x<t[(m+n)/2])
                n=(m+n)/2-1;
            if(x==t[(m+n)/2])
                {
                    printf("1\n%d\n",(m+n)/2);
                    return ;
                }
    	}
    	printf("0\n%d\n%d\n",m,m+1);
    	return ;
    }
    
    

    C++ :

    #include<iostream>
    using namespace std;
    int B(int a[],int x,int n,int &i,int &j)
    	{    
    		int left=0;
    	    int right=n-1;
    		while(left<=right)
    		{
    			int middle=(left+right)/2;
    				if(x==a[middle])
    					return middle;
    				if(x>a[middle])
    				left=middle+1;
    				else
    				right=middle-1;
    	
    		}
    		i=right;
    		j=left;
    		return -2;
    	}
    int main()
    	{
    	
            int x,n;
    		int i=0;
    		int j=0;
    		cin>>x>>n;
    		int a[1000];
    		for(i=0;i<n;i++)
    		cin>>a[i];
    		int C=B(a,x,n,i,j);
    	if(C>=0)
    		cout<<1<<"\n"<<C<<endl;
    	else
    		cout<<0<<"\n"<<i<<"\n"<<j<<"\n";
    }
    

    Java :

    
    
    import java.util.Scanner;
    
    public class Main
    {
        /**
         * 
         * BinarySearch Demo
         * 
         * @param sortedArray
         * @param targetElement
         *            -the element you what to find
         * 
         * @return the position of the finded value in array
         * 
         */
        public static int BinarySearchDemo(int[] sortedArray, int targetElement)
        {
            int left = 0;
            int right = sortedArray.length - 1;
            int middle = 0;
            while (left <= right)
            {
                middle = (left + right) / 2;
    
                if (targetElement == sortedArray[middle])
                {
                    // System.out.println("the position of  target is " + middle);
                    System.out.println(1);
                    System.out.println(middle);
                     return middle;
                   
                }
                else if (targetElement > sortedArray[middle])
                {
                  
                    left = middle + 1;
                }
                else
                {
                   
                    right = middle - 1;
                }
    
            }
    
            if (middle - 1 < 0)
            {
                // System.out.println("the target element is not in the array ,the position of  smallest element which bigger than target is: "
                // + (middle));
                System.out.println(0);
                System.out.println(middle);
            }
            else if (middle + 1 == sortedArray.length)
            {
                // System.out.println("the target element is not in the array ,the position of  biggist element which smaller than target is: "
                // + (middle));
                System.out.println(0);
                System.out.println(middle);
            }
    
            else
            {
                if (sortedArray[middle] < targetElement)
                {
                    System.out.println(0);
                   // System.out.println("right " + (middle + 1));
                    System.out.println(middle);
                    System.out.println(middle + 1);
                  //  System.out.println("left " + (middle));
                   
                }
                if (sortedArray[middle] > targetElement)
                {
                  //  System.out.println("right " + (middle));
                    System.out.println("right " + (middle-1));
                    System.out.println("right " + (middle));
                  //  System.out.println("left " + (middle - 1));
                }
            }
            //-1 for find nothing
            return  - 1;       
    
        }
    
        public static void main(String[] args)
        {
            Scanner scanner=new Scanner(System.in);
           int target= scanner.nextInt();
           int number=scanner.nextInt();
           
            
            int[] testArray = new int[number] ;
            for(int i=0;i<number;i++)
            {
                testArray[i]=scanner.nextInt();
            }
            BinarySearchDemo(testArray, target);        
        }
    
    }
    
    
    • 1

    信息

    ID
    973
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者