当前位置:   article > 正文

Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for_index -1 out of bounds for length 3

index -1 out of bounds for length 3

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main{
  4.     static int N = 100010;
  5.     static int n,m;
  6.     static int skill[] = new int[N];
  7.     static int minus[] = new int[N];
  8.     
  9.     public static void main(String[] args){
  10.         Scanner sc = new Scanner(System.in);
  11.         n = sc.nextInt();
  12.         m = sc.nextInt();
  13.         long cnt = 0;                             //long型
  14.         for(int i = 0; i < n; i--){
  15.             skill[i] = sc.nextInt();
  16.             minus[i] = sc.nextInt();
  17.             cnt += (skill[i] - 1)/ minus[i] + 1;     //向上取整
  18.         }
  19.         
  20.         m = (int)Math.min((long)m,cnt);          //类型转换
  21.         int l = 0, r = (int)1e6, mid = 0;
  22.         while(l < r){
  23.             mid = l + r + 1 >> 1;                //二分模板
  24.             if(check(mid)) l = mid;
  25.             else r = mid - 1;
  26.         }

原因是第一个for循环处,将i++ 写成了 i--

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/879810
推荐阅读
相关标签
  

闽ICP备14008679号