此代码在代码块中完美运行,但在 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
这段代码是来自hackerearth的问题。问题的名称是"Prateek和他的朋友"请帮忙。
我不确定这是否是您唯一的错误,但您应该始终确保已将整数变量分配给(除非您最终不使用它们(在这种情况下,明智的默认值可能是 long int n = 0, x = 0
.
此外,如果cin>>n
无法从流中读取(由于 EOF(,您将无法正常工作,您可能应该在执行读取后检查它是否成功,使用 cin.fail()
,如果它返回比上一个操作(或之前的操作(失败true
。
我实际上不记得以前处理过流读取失败的可能性,所以我可能错了/错过了一些东西
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么QueryInterface查看两个不同的com项目的同一代码行
- JNI 代码返回字符串,但得到错误 java.lang.UnsatisfiedLinkError: com.packag
- 如何在 JavaScript 代码中调用 COM 方法 (c++)
- 如何从com库中打印到主机,并从.NET代码召唤激活器
- 为使用C# /托管代码的无注册COM Interop设置DLL
- 使用本机/C++代码在VS2010中创建简单COM对象的最简单方法是什么
- 此代码在代码块中完美运行,但在 ideone.com 时出现运行时错误
- 为什么这个C++代码会让 SIGILL ideone.com
- 如何检测 C++/COM 中无法访问的代码
- 通过 .net 互操作类调试到 COM C++代码
- COM 代码库位置 - 如何选择要使用的版本
- 正在将COM对象添加到托管代码中
- 从c#中定义的COM类返回成功代码
- 是否有可能在静态库中编写COM代码,然后将其链接到DLL ?< / h1 >
- 为什么这个COM代码会泄漏
- 当64位非托管代码通过COM调用托管代码时会发生什么
- 如何在不注册dll的情况下对c++ COM代码进行单元测试?
- 我应该如何写正确的COM代码
- 使用COM从c++代码封送c#结构体