简体   繁体   English

这段代码可以在代码块中完美运行,但在ideone.com上给出了运行时错误

[英]This code is running perfectly in codeblocks but giving a runtime error on ideone.com

int main()
{

    long int n,x,cost[100];
    cin>>n>>x;
    for(int i=0;i<n;i++)
        cin>>cost[i];

    int i=0,flag=0,s=0,first;
    first=cost[0];
    while(i<n)
    {
       s+=cost[i];
       if(s>x){
           s-=first;
           first++;
       }
       if(s==x)
       {
           flag=1;
           break;
       } 
       i++;
    }
    if(flag==0) cout<<"no";
    else cout<<"yes";
}

Ideone demo Ideone演示

This code is a problem from hackerearth. 这段代码来自hackerearth。 Name of the problem is 'Prateek and his friends' Please help. 问题的名字是“ Prateek和他的朋友们”,请帮忙。

I am not sure if this is your only error, but you should always ensure that integer variables have been assigned to, (unless you don't end up using them) In this case a sensible default might be long int n = 0, x = 0 . 我不确定这是否是您唯一的错误,但您应始终确保已将整数变量分配给(除非最终不使用它们)在这种情况下,明智的默认值可能是long int n = 0, x = 0

In addition, cin>>n will not work properly if it can't read from the stream (due to EOF) you should probably check after doing a read that it succeeded, use cin.fail() , if it returns true than the previous operation (or a one before that) failed. 此外,如果cin>>n无法从流中读取(由于EOF),则您将无法正常工作(由于EOF),您应该在读取成功后进行检查,请使用cin.fail() ,如果它返回的true大于之前的操作(或之前的操作)失败。

I don't actually recall dealing with the possibility of a stream read failing before, so I may be wrong/missed something 我实际上不记得以前曾处理过流读取失败的可能性,因此我可能是错的/遗漏了某些内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个C ++代码在ideone.com上获得SIGILL? - Why does this C++ code get SIGILL on ideone.com? 为什么ideone.com这样做? - Why ideone.com does this? 当我在ideone.com上运行时,在阶乘中尾随零。 甚至SPOJ也不接受 - trailing zeros in a factorial.when i run it on ideone.com i'm getting runtime error with infinite output. Even SPOJ is also not accepting 如何为ideone.com中的代码文件指定自定义文件名? - How can I give a custom file names to a code file in ideone.com? C++ 正则表达式在 ideone.com 上匹配,但在 Android NDK 构建中不匹配 - C++ regex matches on ideone.com but not in Android NDK build 程序在Visual Studio 2012中运行,但不在ideone.com中运行 - Program runs in Visual Studio 2012 but not ideone.com 为什么这会在ideone和visual studio中显示运行时错误,但在代码块中却没有? - why this shows runtime error in ideone & visual studio but not in code blocks? 无法弄清楚我的程序在spoj上给出运行时错误但在ideone上给出运行时错误的原因 - Can't figure out the reason why my program is giving runtime error on spoj but not on ideone 在ideone上运行SIGILL,但在Codeblocks上运行警告 - SIGILL on ideone but runs with warning on Codeblocks 以下代码完美地结合在一起,但显示运行时错误。 为什么? - following code comipling perfectly but showing runtime error . why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM