1-5 re 6ac(java)求助!

P2249 【深基13.例1】查找

Mr_xiao @ 2023-03-07 19:46:04

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
    static StreamTokenizer streamTokenizer=new StreamTokenizer( new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter printWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static int nextInt() throws IOException {
        streamTokenizer.nextToken();
        return (int) streamTokenizer.nval;
    } 
    public static char nextchar() throws IOException {
        streamTokenizer.nextToken();
        return (char) streamTokenizer.nval;
    }
    public static String nextString() throws IOException {
        streamTokenizer.nextToken();
        return streamTokenizer.sval;
    }
    static long [] array,ac;
    static long e;
    static int l,r,mid,numbre,c;
    public static void main(String[] args) throws IOException {
    int n=nextInt();
    int m=nextInt();
    array=new long[n];
    ac=new long[m];
    for (int i = 0; i < array.length; i++) {
        array[i]=nextInt();
    }
    for (int i = 0; i < m; i++) {
        ac[i]=nextInt();
    }
    for (int i = 0; i < m; i++) {
            printWriter.print(leftMargin(array, ac[i])+" ");
    }
    printWriter.flush();
    }
    public static int leftMargin(long []nums,long target) {
    l=0;r=nums.length;
    while(l<=r) {
        mid=(r-l)/2+l;
        long temp=nums[mid];
        if (temp==target) {
            r=mid-1;
        }else if (temp>target) {
            r=mid-1;
        }else {
            l=mid+1;
        }
    }
    if (nums[l]==target) {
        return l+1;
    }else {
        return -1;
    }

    }

}

by KKKZOZ @ 2023-03-07 20:49:28

RE应该是因为数组访问越界了

这里的问题:

nums[l]==target

我小改了一下

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
    static StreamTokenizer streamTokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static int nextInt() throws IOException {
        streamTokenizer.nextToken();
        return (int) streamTokenizer.nval;
    }

    public static char nextchar() throws IOException {
        streamTokenizer.nextToken();
        return (char) streamTokenizer.nval;
    }

    public static String nextString() throws IOException {
        streamTokenizer.nextToken();
        return streamTokenizer.sval;
    }
    static int n,m;
    static int[] array;
    static long e;
    static int l, r, mid, numbre, c;

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        array = new int[n+1];
        for (int i = 1; i <= n; i++) {
            array[i] = nextInt();
        }
        for (int i = 0; i < m; i++) {
            int ac = nextInt();
            printWriter.print(leftMargin(array, ac) + " ");
        }
        printWriter.flush();
    }

    public static int leftMargin(int[] nums, int target) {
        l = 1;
        r = n;
        while(l<r){
            mid = l + (r-l)/2;
            if(nums[mid]>=target)
                r = mid;
            else 
                l = mid+1;
        }
        if(l==n+1){
            return -1;
        }else if(nums[l]!=target){
            return -1;
        }else 
            return l;
    }

}

by Mr_xiao @ 2023-03-07 23:45:15

@KKKZOZ 好


|