首页 > 解决方案 > java中Hackerearth问题中的NZEC错误

问题描述

我正在尝试解决这个黑客地球问题https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/anagrams-651/description/

我曾尝试通过互联网搜索,但找不到解决我问题的理想解决方案

这是我的代码:

String a = new String();
String b = new String();
a = sc.nextLine();
b = sc.nextLine();

int t = sc.nextInt();
int check = 0;
int againCheck =0;
for (int k =0; k<t; k++)
{
    for (int i =0; i<a.length(); i++)
    {
        char ch = a.charAt(i);       
        for (int j =0; j<b.length(); j++)
        {
            check =0;
            if (ch != b.charAt(j))
            {
                check=1;                      
            }

        }
        againCheck += check;           
    }
}

System.out.println(againCheck*againCheck);

我希望输出为 4,但它显示“NZEC”错误

任何人都可以帮助我吗?

标签: java

解决方案


要求规定1输入是一个数字 (N),后跟 2 x N 行。您的代码正在读取两个字符串,后跟一个数字。InputMismatchException当它试图将输入的第 3 行解析为数字时,它可能会抛出一个错误。

提示:

  • 仔细阅读要求是值得的。
  • 阅读 CodeChef 上有关如何调试 NZEC 的文章:https ://discuss.codechef.com/t/tutorial-how-to-debug-an-nzec-error/11221 。它解释了诸如在代码中捕获异常和打印出 Java 堆栈跟踪等技术,以便您了解发生了什么问题。

1 - 诚然,要求并不十分明确。但是在示例输入中,第一行是一个数字。


推荐阅读