这是代码,在我的机器上工作正常。但是在Codechef中提交它时会出现一个名为NZEC错误的问题,我不熟悉它并且不知道如何调试它。哪里错了?
问题:https://www.codechef.com/problems/SNELECT
import java.io.*;
import java.util.*;
class A
{
public static void main(String args[]) throws IOException
{
int t,i;
Scanner sc= new Scanner(System.in);
t=sc.nextInt();
while(t>0) //testcases
{
int eaten=0,m=0,s=0;
String a=new String();
a=sc.next();
int n= a.length();
for( i=0;i<n;i++)
{
if(a.charAt(i)=='m') //counting_m&s
m++;
else if(a.charAt(i)=='s')
s++;
}
if(a.charAt(0) == 'm' && a.charAt(1)=='s')
{
eaten++; //mongoos eaten atmost 1 snake
a=a.substring(0,1)+'x'+a.substring(2); //removing eaten snake from string
}
if(a.charAt(n-1) == 'm' && a.charAt(n-2)=='s')
{
eaten++;
a=a.substring(0,n-2)+'x'+a.substring(n-1);
}
for( i=1;i<n-1;i++)
{
if(a.charAt(i) == 'm' && a.charAt(i-1)=='s')
{
eaten++;
a=a.substring(0,i-1)+'x'+a.substring(i);
}
else if(a.charAt(i) == 'm' && a.charAt(i+1)=='s')
{
eaten++;
a=a.substring(0,i+1)+'x'+a.substring(i+2);
}
}
if(s-eaten < m)
System.out.println("mongooses"); //checking the winner
else if(s- eaten > m)
System.out.println("snakes");
else
System.out.println("tie");
t--;
}
}
}
答案 0 :(得分:0)
NZEC是运行时错误。 当访问负数组索引或编写的程序占用的空间超过分配给我们的程序运行的内存时,通常会发生这种情况。
try{
// code which you think might throw exception
}catch(java.lang.Throwable t){
// code to catch that exception or simply don't give any code leave it blank
}
Throwable类是Java中所有错误和异常类的超类。