比如a[]={2,4,5,6,7},得出的两组数{2,4,6}和{5,7},abs(sum(a1)-sum(a2))=0;
比如{2,5,6,10},abs(sum(2,10)-sum(5,6))=1,所以得出的两组数分别为{2,10}和{5,6}。
vector<int> vct; int last = INT_MAX; int halfOfSum(int* arr, int len) { int sum = 0; for (int i = 0; i < len; ++i) { sum += arr[i]; } return sum / 2; } void difPrint(int* arr, int len, vector<int> vct) { for (int i = 0; i < vct.size(); ++i) { cout << vct[i] << " "; } cout << endl; int vid = 0; for (int i = 0; i < len; ++i) { if (vct[vid] == arr[i]) { vid++; continue; } else { cout << arr[i] << " "; } } cout << endl << endl; } void findCloestSequence(int* arr, int len, int step, int cur, vector<int> v) { if (!arr || !len || cur < 0 || step == len) { return; } else if (cur < last) { last = cur; vct = v; if (!last) { return; } } v.push_back(arr[step]); findCloestSequence(arr, len, step + 1, cur - arr[step], v); v.pop_back(); findCloestSequence(arr, len, step + 1, cur, v); }