0
Follow
0
View

# 长度为100010的索引-1越界

drlowa 注册会员
2023-02-28 01:15

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > have found the problem, you write wrong in the for loop conditions, should be the for(int I = 0; i < n; i++) instead of for(int i = 0; i < n; i--), because i-- keeps decreasing the value of i, causing the index to go out of bounds.

You can set for(int i = 0; i < n; i--){change to for(int i = 0; i < n; i++){will solve this problem.

csfxxaq 注册会员
2023-02-28 01:15
• You can see the manual: < a href = "https://zh.cppreference.com/w/c/language/main_function" target = "_blank" > < font size = "3" > c language - the main function < / font > < / a >
xudong9 注册会员
2023-02-28 01:15

According to the error message index-1 out of bounds for length 100010, it is likely that access to the array went out of bounds because the array Index had a negative value. Based on the for loop in the code, we can see that the index variable i of the loop is decreasing, not increasing. This means that in the loop, the value of i decreases until it is less than 0, resulting in a negative array index, which triggers an array out-of-bounds error.

Therefore, to fix this error, simply change the decrement operation in the for loop to an increment operation. Specifically, the code for(int i = 0; i < n; i--){for(int i = 0; i < n; i++){will do.

Here is an example of the corrected code:

``````import java.util.*;
import java.io.*;

public class Main{
static int N = 100010;
static int n,m;
static int skill[] = new int[N];
static int minus[] = new int[N];

public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
long cnt = 0;
for(int i = 0; i < n; i++){
skill[i] = sc.nextInt();
minus[i] = sc.nextInt();
cnt += (skill[i] - 1) / minus[i] + 1;
}

m = (int)Math.min((long)m, cnt);
int l = 0, r = (int)1e6, mid = 0;
while(l < r){
mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
}

// 根据题目描述写出 check 函数的代码
public static boolean check(int mid){
// TODO: 实现 check 函数
return true;
}
}

``````

Publish Time
2023-02-28 01:15
Update Time
2023-02-28 01:15