我想确定两个序列是否“等价”,即两个序列具有相同性的 a 和 b 是等价的元素的数量,并且它们都包含少于两个的数字,或者对于所有i,j,1 <= i,j <= n,其中,n是序列的长度a [i] <= a [j]当且仅当b [i] <= b [j]
我已经用C语言编写了一个小程序以及一组测试用例
include <stdio.h>
int main() {
// Should be true, and is
// int a[1] = {12};
// int b[1] = {8};
// Should be true, and is
// int a[3] = {1, 2, 3};
// int b[3] = {12, 32, 43};
// Should be true, and is
// int a[3] = {12, 45, 78};
// int b[3] = {1, 2, 3};
// Should be true, and is
// int a[6] = {7, 5, 9, 13, 12};
// int b[6] = {4, 2, 25, 33, 26};
// Should be true, and is
// int a[3] = {1, 1, 1};
// int b[3] = {1, 1, 1};
// Should be false, and is
// int a[4] = {12, 1, 2, 3}; // because there is a decrease between 12 and 1
// int b[4] = {3, 4, 5, 6}; // but an increase between 3 and 4
// Should be true, but for some reason is not
int a[6] = {23, 55, 22, 33, 9, 18};
int b[6] = {21, 43, 11, 22, 7, 11};
int size_of_arrays = 6; // set this to the correct value depending on values being tested
int are_equivalent = 1;
for (int m = 0; m < size_of_arrays; m = m + 1){
for (int n = 0; n < size_of_arrays; n = n + 1){
if ((a[m] <= a[n]) != (b[m] <= b[n])){
are_equivalent = 0;
}
}
}
if (are_equivalent == 1) {
printf("The two sequences are order equivalent.");
} else {
printf("The two sequences are not order equivalent.");
}
return 0;
}
但是最后一个测试用例尤其使我陷入循环(双关语意)。在某些情况下,我的逻辑没有涵盖,但我看不到它。有什么想法可能会丢失吗?