Given an array of
N
size. The task is to rotate array byd
elements whered
is less than or equal toN
.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
最佳答案
刚刚在 Rotating an Array | Geek for Geeks 上提交了您的确切代码(问题中给出的站点)。它工作完美,没有遇到运行时错误。
关于c++ - 旋转数组 :Segmentation Fault (SIGSEGV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56696430/