我已经为数组旋转编写了一个cpp代码,文件处理部分对我来说有点棘手。代码本身是正确的,但即使文件与代码在同一目录中,由于某种原因也不起作用
#include <math.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
using namespace std;
int ar[100];
// #define crap ios_base::sync_with_stdio(false);cin.tie(NULL);
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void leftRotate(int arr[], int d, int n)
{
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
int temp = arr[i];
int j = i;
while (1) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
//int *func(int m)
//{
// int *p;
// p=new int[m];
// return (p);
//}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
// crap;
int n,d;
cin>>n>>d;
// ar=func(n);
// cout<<sizeof(ar);
for (int i=0;i<n;i++)
{
cin>>ar[i];
}
leftRotate(ar, d, n);
for (int i = 0; i < n; i++)
cout << ar[i] << " ";
return 0;
}
代码本身正在工作,并在终端中返回正确的输出,但我似乎在这里找不到问题我尝试过不进行文件处理,它在终端上给出了返回值3221225620样本输入为:
5 2
1 2 3 4 5
这里显示了一个可能的错误:
3221225620 (0xC0000094): Zero Division Error
意味着代码中的除数有时可能为零。
至于您的代码(第20行:d = d % n;
(,当您的n
是0
时,输出将显示return value 3221225620
所以请在"input.txt"
中检查您的数据