两个数组:
a[] = {1 2 3 4}
b[] = {3 4 1 2}
底部数组只是顶部数组向右移动了两个位置。如果顶部数组可以右移以创建底部数组,我们称它们为等价移位。
这是我尝试创建一个函数(我需要使用布尔函数)来确定两个数组是否“移位等效”:
#include <iostream>
using namespace std;
bool equivalent(int a[], int b[], int size) {
int value; // if 1 returns as truth
int k; // counter to compare both arrays
for (int j = 0; j <= size; j++) {
for (int i = 0; i <= size; i++) {
a[i] = a[i + j];
}
}
for (k = 0; k <= size; k++) {
if (a[k] != b[k]) {
value = 0;
} else value = 1;
}
return (value == 1);
}
int main() {
int n;
cout << "Please input a size " << endl;
cin >> n;
int *mtrx = new int[n];
int *mtrx1 = new int[n];
int x;
for (x = 0; x < n; x++) {
cout << "Please make entries for the first array: " << endl;
cin >> mtrx[x];
}
x = 0;
for (x = 0; x < n; x++) {
cout << "Please make entries for the 2nd array: " << endl;
cin >> mtrx1[x];
}
bool answr = equivalent(mtrx, mtrx1, n = n - 1);
if (answr) {
cout << "They are shift equivalent." << endl;
} else {
cout << "They are not shift equivalent." << endl;
}
delete[] mtrx;
delete[] mtrx1;
system("PAUSE");
return 0;
}
当我执行我的程序时,我使用array1 = {1 2 3}
andarray2 = {3 1 2}
来测试班次等价性。他们应该是,但我的程序说他们不是。