c++ - 旋转数组 :Segmentation Fault (SIGSEGV)

标签 c++ arrays segmentation-fault

Given an array of N size. The task is to rotate array by d elements where d is less than or equal to N.

Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 200 1 ≤ A[i] ≤ 1000

Example input:

1
5
1 2 3 4 5
2

Output

3 4 5 1 2

我编写的程序似乎是合法的,但是当我尝试运行它时出现了段错误。我什至运行了上面的例子,我得到了正确的输出。 来源是GeeksforGeeks: Rotating and Array .

#include <bits/stdc++.h>
using namespace std;

int main() {
  int test_case, numb, from, arr[200];

  cin >> test_case;
  while (test_case--) {
    cin >> numb;

    for (int i = 0; i < numb; i++) {
      cin >> arr[i];
    }

    cin >> from;
    for (int i = from; i < numb; i++) {
      cout << arr[i] << " ";
    }
    for (int j = 0; j < from; j++) {
      cout << arr[j] << " ";
    }

    cout << "\n";
  }
  return 0;
}

我的代码需要哪些更改?我该怎么做才能避免将来出现此类错误? click_to_see_segment_fault

最佳答案

enter image description here刚刚在 Rotating an Array | Geek for Geeks 上提交了您的确切代码(问题中给出的站点)。它工作完美,没有遇到运行时错误。

关于c++ - 旋转数组 :Segmentation Fault (SIGSEGV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56696430/

相关文章:

c++ - 动态转换如何工作?

c++ - C++ 中的组合运算符重载?

php - 从 php select 创建多维数组

c# - 如何从数组填充组合框?

使用指向 uint64_t 的指针复制 C 中的自定义字符串会导致段错误

java - JVM 在 Lucene DataInput.readVInt 上崩溃

c++ - 带有自定义 vector 的 set_interaction 示例不起作用

c++ - 大数组的 ba​​d_alloc

arrays - MongoDB 从不在其他集合中的集合中获取文档

调用方法失败 : segmentation fault in c