此代码完美地适用于T的短值(例如1,10,5)
但是它为更大的值(例如100)提供了运行时错误(NZEC)。
该错误的解决方法是什么?
谢谢您的帮助:)
这是问题的链接:
https://www.codechef.com/MARCH20B/problems/CHPINTU
这是示例输入:
1
6 4
1 2 3 3 2 2
7 3 9 1 1 1
该程序将针对此示例输入成功运行。
下面是代码:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int M, N, T;
int[] F, P;
T = sc.nextInt();
while (T-->0) {
N = sc.nextInt();
M = sc.nextInt();
F = new int[N];
P = new int[N];
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
if (x <= M) {
F[i] = x;
} else {
break;
}
}
// for (int i = 0; i < N; i++) {
// System.out.print(F[i] + " ");
// }
// System.out.println();
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
if (x <= 50 && x >= 1) {
P[i] = x;
}
}
// for (int i = 0; i < N; i++) {
// System.out.print(P[i] + " ");
// }
// System.out.println();
int maxVal[] = new int[M];
for (int i = 0; i < N; i++) {
maxVal[F[i]] += P[i];
}
// for (int i = 1; i < M; i++) {
// System.out.print(maxVal[i] + " ");
// }
// System.out.println();
int count = 0;
for (int i = 1; i < M; i++) {
for (int j = 1; j < M; j++) {
if (maxVal[i] < maxVal[j]) {
count = maxVal[i];
} else if (maxVal[j] < maxVal[i]) {
count = maxVal[j];
}
}
}
System.out.println(count);
}
}
}