[英]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";
}
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.