【发布时间】:2013-01-16 06:50:24
【问题描述】:
两个数组:
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} 和array2 = {3 1 2} 来测试移位等效性。他们应该是,但我的程序说他们不是。
【问题讨论】:
-
你的大括号风格(曾经)很奇怪。
-
我发现空间编辑更奇怪。
-
什么是大括号样式?对不起,我是编程新手。
-
你的逻辑也没有意义..
-
对于大括号样式,您是否注意到在@Rapptz 的编辑之后,所有
{都在表达式之后开始,而}出现在其自身行的开头?这几乎是最常见的样式,并且清楚地显示了块的结束位置。你的风格在最后一个语句的末尾出现了},这非常令人困惑和不明显。
标签: c++ function boolean dynamic-arrays boolean-expression