简单代码中的运行时错误 (NZEC)

发布于 2024-10-26 20:13:38 字数 714 浏览 4 评论 0原文

在 SPOJ 运行以下代码时,出现运行时错误 (NZEC)。如果你们中有人能指出发生了什么,我将非常感激。

//0<=A<=B<=10^18, 1<=N<=10^18
using System;
class any
{
static void Main()
 {
    long t = long.Parse(Console.ReadLine());
    ulong a, b, n;

    for(long k = 0; k < t; k++)
     {
        string[]s = Console.ReadLine().Split(' ');
        a = ulong.Parse(s[0]);
        b = ulong.Parse(s[1]);
        n = ulong.Parse(s[2]);
        Console.WriteLine(diviEntre2(a, b, n));
     }
 }
static ulong diviEntre2(ulong f, ulong c, ulong n)
{
   ulong k, s, m;

    if (f == c && c % n == 0 && f != 0) k = c/n;

    else
     {
      s = f/n;
      m = c/n;

      k = m - s;
     }

  return k;
}
}

I'm getting runtime error (NZEC) when running the following code over at SPOJ. I'd be very thankful if any of you would kindly point out what's going on.

//0<=A<=B<=10^18, 1<=N<=10^18
using System;
class any
{
static void Main()
 {
    long t = long.Parse(Console.ReadLine());
    ulong a, b, n;

    for(long k = 0; k < t; k++)
     {
        string[]s = Console.ReadLine().Split(' ');
        a = ulong.Parse(s[0]);
        b = ulong.Parse(s[1]);
        n = ulong.Parse(s[2]);
        Console.WriteLine(diviEntre2(a, b, n));
     }
 }
static ulong diviEntre2(ulong f, ulong c, ulong n)
{
   ulong k, s, m;

    if (f == c && c % n == 0 && f != 0) k = c/n;

    else
     {
      s = f/n;
      m = c/n;

      k = m - s;
     }

  return k;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

秋凉 2024-11-02 20:13:39

对于 Java,当代码抛出异常时返回 NZEC。对于 Spoj 等问题,如果测试用例不是由标识符字符串终止,则输入中的最后一行通常会导致此异常。

对于这种情况,一个有用的技巧是将代码包装在 try - catch 中,并在出现异常时简单地返回。捕获的异常表明您已到达输入末尾。

public static void main(String[] args) {
    temp program = new temp();
    try{
    program.begin();
    } catch(Exception e){
        return;
    }
}

For Java, NZEC is returned when the code throws an exception. For problems on Spoj, etc often the last line in the input causes this exception if the test cases are not terminated by an identifier string.

For such cases, a useful hack is to wrap your code in a try - catch and simply return if there's an exception. The caught exception signals that you've reached the end of input.

public static void main(String[] args) {
    temp program = new temp();
    try{
    program.begin();
    } catch(Exception e){
        return;
    }
}
千年*琉璃梦 2024-11-02 20:13:39

我在用 java 编程时也收到了同样的消息。事实证明,我应该将源代码放在默认包中(或者根本不更改包)。我希望这对某人有帮助。

I had the same message while programming in java. It turned out I should have put my source code in default package (or not change package at all). I hope this helps someone.

救星 2024-11-02 20:13:39

我不知道当 main 函数为 void 时 java 返回什么,但这可能是此错误消息的原因。 Spoj 还会检查程序的返回值,它期望 0(成功/非错误代码)。
我想将 main 函数更改为 return 0 将修复此错误消息。

我刚刚在 C 程序中遇到了同样的错误,并且添加 return 0 将错误更改为接受。

I don't know what java returns when the main function is void, but this can be the reason of this error message. Spoj also checks the return value of your program, and it expects 0 (success/non-error code).
I guess changing your main function to return 0 will fix this error message.

I just had this same error with a C program, and adding a return 0 changed the error to accepted.

眉黛浅 2024-11-02 20:13:39

此错误也可能意味着程序无法正常工作,即输出不是预期的输出...无论您相信与否,这很可能是您的代码没有执行问题要求的操作...

引用最后给出的链接->

NZEC(非零退出代码)- 帮助判断来自 WA 的崩溃
解释性语言; WA = 错误答案。

请参阅此链接SPOJ 系统

This error can also mean that the program does not work correctly that is the output is not the expected output... believe it or not this is a strong possibility that your code is just not doing what the question asks it to....

Quoting from the link given at the end ->

NZEC (non-zero exit code) - helps telling crash from WA with
interpreted languages; WA = Wrong Answer.

please see this link The SPOJ System

最偏执的依靠 2024-11-02 20:13:39

我在 cpp 程序上获得了 NZEC 的问题“EKO”。我在 int main() 语句之前进行了数组声明。我将声明转移到主函数内,解决方案被接受。

我通常在 main 函数之外声明数组,但在本例中,数组是一个很大的数组 (int array[100001])。因此,可以在 main 中声明您的数组。

I got NZEC on a cpp program for the problem 'EKO'. I was making an array declaration right before the int main() statement. I shifted the declaration inside the main function and the solution got accepted.

I normally have the array declaration outside the main function but in this case the array was a big one (int array[100001]). So may be declare your arrays inside main.

荒岛晴空 2024-11-02 20:13:38

NZEC 代表非零退出代码。对于 C 用户,如果您的 main 方法没有 return 0; 语句,则会生成此错误。其他语言(如 Java/C++)如果抛出异常,也可能会生成此错误。

NZEC stands for Non Zero Exit Code. For C users, this will be generated if your main method does not have a return 0; statement. Other languages like Java/C++ could generate this error if they throw an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文