给定一个整数数组,判断能否从中找出3个数a、b、c,使得他们的和为0,如果能,请找出所有满足和为0个3个数对。
#define SIZE 10
void judgeAndPut(int* arr, int target, int begin, int end) {
while (begin < end) {
if (arr[begin] + arr[end] + arr[target] > 0) {
end--;
} else if (arr[begin] + arr[end] + arr[target] < 0) {
begin++;
} else {
cout << " " << arr[target] << " " << arr[begin] << " " << arr[end]
<< endl;
begin++;
end--;
while (begin + 1 < end && arr[begin - 1] == arr[begin]) {
begin++;
}
while (end - 1 > begin && arr[end + 1] == arr[end]) {
end--;
}
}
}
}
void findThreeSumEq0(int* arr) {
qsort(arr, SIZE, sizeof(int), myCmp);
for (int i = 0; i < SIZE; i++) {
cout << " " << arr[i];
}
cout << endl;
for (int i = 0; i < SIZE - 2; ++i) {
if (i != 0 && arr[i] == arr[i - 1]) {
continue;
}
judgeAndPut(arr, i, i + 1, SIZE - 1);
}
}