为什么我的代码给出NZEC(运行时错误)

时间:2016-10-21 10:37:15

标签: java runtime-error

我的代码在我的系统上正常运行,但在线系统上给出了NZEC

static String fun (String str,int n) throws Exception
{
    String concat="";

    if(n==0)
        return str;
    else
    {
        concat=str+rev(str);
        return fun(concat,(n-1));
    }

}

功能-2

static String rev(String str) throws Exception
{   
    StringBuffer br=new StringBuffer(str);
    br.reverse();
    String revstr=br.toString();
    return revstr;

}

功能主要

public static void main(String[] args) throws IOException {
    try
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int test=Integer.parseInt(br.readLine());

        for(int p=0;p<test;p++)
        {
            String sub="";
            int count=0;
            String[] line=br.readLine().split(" ");
            String finalstr=fun(line[0],Integer.parseInt(line[1]));
            int length=finalstr.length();

            for( int c = 0 ; c < length ; c++ )
            {
                for( int i = 1 ; i <= length - c ; i++ )
                {
                    sub = finalstr.substring(c, c+i);
                    int subcheck=Integer.parseInt(sub)%1000000007;

                    if(subcheck%3==0)
                    {
                        count++;

                    }
                }
            }
            System.out.println(count);

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

我在Hackerearth上尝试了你的代码:请参阅:https://code.hackerearth.com/c4cfd4e?key=dd429a7c1e29beb803f5efe4fcd7ec37

其工作

我不知道您的输入..但我认为在此行中解析非常大的数字时会遇到问题:

            int subcheck=Integer.parseInt(sub)%1000000007;

我添加了捕获NumberFormatException以查看更多详细信息..