[英]Why I am getting different output on HackerRank than the output of my IDE?
I am trying to solve a problem named "Jumping on the Clouds" on HackerRank.我正在尝试解决 HackerRank 上名为“在云端跳跃”的问题。
I have written a code primarily and it gives the right output as my expectations.我主要编写了一个代码,它按照我的期望提供了正确的输出。 But when I am submitting the code on HackerRank it gives different output with the same input.但是当我在 HackerRank 上提交代码时,它会以相同的输入提供不同的输出。 How it is possible!怎么可能!
I tried to compile in different IDE and text editors like CodeBlocks, VSCode, and an online compiler(Ideone) and they give the correct output but HackerRank is showing a different output.我尝试在不同的 IDE 和文本编辑器(如 CodeBlocks、VSCode 和在线编译器 (Ideone))中进行编译,它们给出了正确的输出,但 HackerRank 显示了不同的输出。
My code (C++):我的代码(C++):
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
for(int i = 0; i < n; i++) {
if(arr[i + 2] == 0) {
count++;
i = i + 1;
} else if(arr[i + 1] == 0) {
count++;
} else {
continue;
}
}
cout << count << endl;
return 0;
}
Outputs: Output on VSCode:输出: VSCode 上的输出:
Output on Ideone: Ideone 上的输出:
Output on HackerRank: HackerRank 上的输出:
Problem link: Jumping on the Clouds问题链接: 在云端跳跃
Where is the problem and how to solve it?问题出在哪里以及如何解决?
Your code has undefined behavior because arr
is accessed out of bounds.您的代码具有未定义的行为,因为arr
被越界访问。 The loop constraint is i < n
, but you access arr[i + 2]
and arr[i + 1]
.循环约束是i < n
,但您可以访问arr[i + 2]
和arr[i + 1]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.