問題描述
5, 5,-7, 5, 9, -1, 5, 1, 9, 4, 6 這堆數中兩個數的和為10的組合有:5+5, 9+1, 4+6,如何快速的找出這樣的組合?
假定
陣列a[]存放元素,陣列大小為len_a
指定和為aim
思路一
先排序,low=0(最低位置),up=len_a(最高位置)
- 當a[low]+a[up]>aim時,hig=high-1
- 當a[low]+a[up]<aim時,low=low+1
- 當a[low]+a[up]=aim時,輸出a[low]、a[up]
程式碼:
#include <iostream> #include <algorithm> using namespace std; void printPairSums(int data[], int size, int sum); int main(int argc, char* argv[]) { int data[] = {1, 5, 9, -1, 4, 6, -2, 3, -8}; int size = sizeof(data) / sizeof(data[0]); int i; sort(data, data + size); printPairSums(data, size, 8); return 0; } void printPairSums(int data[], int size, int sum) { int first = 0; int last = size -1; int s = 0; while (first < last) { s = data[first] + data[last]; if (s == sum) { cout << data[first] << " + " << data[last] << " = " << sum << endl; first++; last--; } else if (s < sum) { first++; } else { last--; } } }
思路二
位操作(詳細解釋看http://www.cnblogs.com/kaituorensheng/p/3169570.html)
思路:
#include <iostream> #include <algorithm> using namespace std; void setBit(char *entry, int nBits) { entry[nBits/8] = entry[nBits/8] | (1 << (nBits%8)); } void setBit_0(char *entry, int nBits) { entry[nBits/8] = entry[nBits/8] & ~(1 << (nBits%8)); }
int checkBit(char *entry, int nBits) { return entry[nBits/8] & (1 << (nBits%8)); } int main() { int data[] = {5,-7, 9, -1, 1, 9, 4, 6}; int aim = 10; int size = sizeof(data) / sizeof(data[0]); int i, min=data[0], max=data[0]; int num_aim = 0; if(aim % 2 == 0) { for(i=0; i<size; i++) { if (aim / 2 == data[i]) num_aim += 1; } } for(i=1; i<size; i++) { if(data[i] < min) min = data[i]; if(data[i] > max) max = data[i]; } int dis_e = (max-min) / 8 + 1; char entry[dis_e]; for (i=0; i<dis_e; i++) { entry[i] = 0; } int dis = 0 - min; for(i=0; i<size; i++) { setBit(entry, data[i]+dis); } if(aim % 2==0) { setBit_0(entry, aim/2 + dis); if(num_aim > 1) cout << data[i] << " " << (aim - data[i]) << endl; } for(i=0; i<size; i++) { if(checkBit(entry, aim - data[i] + dis) != 0) { setBit_0(entry, data[i] + dis); setBit_0(entry, aim - data[i] + dis); cout << data[i] << " " << (aim - data[i]) << endl; } } }
問題擴充套件
已知兩個升序陣列,從兩個陣列中各取一個數值,求使得兩個數之和為給定值的所有組合。
問題本質和一位陣列一樣,一個從一個陣列的開始前進,一個從另外一個陣列的最後後退。