代码之家  ›  专栏  ›  技术社区  ›  sahil mehta

如何解决Codechef上的NZEC错误?

  •  0
  • sahil mehta  · 技术社区  · 7 年前

    问题: 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--;
            }
       }
          }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Tanisha Jain    5 年前

    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中所有错误和异常类的超类。