gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:46:31 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com