求助,这么写为什么过不了呢?

P2249 【深基13.例1】查找

A_jing @ 2023-06-22 23:15:10


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[] arr=new int[n];
        for(int i=0;i<n;++i) {
            arr[i]=sc.nextInt();
        }
        int goal;
        for(int i=0;i<m;++i) {
            goal=sc.nextInt();
            int l=0,r=n-1;
            while(l<r) {
                int mid=l+(r-l)/2;
                if(arr[mid]<goal) l=mid+1;
                else r=mid;
            }
            if(arr[l]==goal) System.out.print(l+1+" ");
            else System.out.print(-1+" ");
        }
        sc.close();
    }

}

by MC00101 @ 2023-07-12 15:04:41


by MC00101 @ 2023-07-12 15:07:40


|