我的目标是找到资金来给定的总所有可能的组合。 例如,如果阵列是 2 59 3 43 5 9 8 62 10 4,如果总为12,则可能的组合
2 10
3 9
8 4
5 3 4
下面是第一套code,我写。想知道可以在这样做的最好的改善。
INT find_numbers_matching_sum(INT * number_coll,诠释总计)
{
INT * search_till = LOWER_BOUND(number_coll,number_coll + TOTAL_SIZE,总计);
INT位置= search_till - number_coll;
如果(* search_till个总与功放;&安培;地点> 0)
{
- 位置;
}
而(地点> = 0)
{
find_totals(number_coll,总共位置);
- 位置;
}
返回1;
}
INT find_totals(INT * number_coll,诠释总,INT END_LOCATION)
{
INT left_ones =总 - number_coll [END_LOCATION]
INT curloc = END_LOCATION;
INT * search_till = 0;
INT位置;
诠释all_numbers [10];
INT I = 0;
all_numbers [i] = number_coll [END_LOCATION]
而(left_ones&安培;&安培; curloc> = 0)
{
search_till = LOWER_BOUND(number_coll,number_coll + END_LOCATION,left_ones);
位置= search_till - number_coll;
如果(* search_till> left_ones和放大器;&安培;地点> 0)
{
- 位置;
}
否则,如果(left_ones< * search_till)
{
打破;
}
curloc =位置;
left_ones = left_ones - number_coll [curloc]
all_numbers [++ i] = number_coll [curloc]
END_LOCATION = curloc - 1;
}
如果(!left_ones)
{
而(ⅰ> = 0)
{
COUT<< all_numbers [我 - ]<< '';
}
}
COUT<< ENDL;
返回1;
}
解决方案
的#include<的iostream>
#包括<载体>
使用名字空间std;
结构状态{
INT伏;
常量州*休息;
无效转储()const的{
如果(休息){
COUT<< ''<<伏;
REST->转储();
} 其他 {
COUT<< ENDL;
}
}
国家():V(0),休息(0){}
国家(INT _V,常量状态和放大器; _rest):V(_V),休息(安培; _rest){}
};
无效SS(INT * IP,为int *年底,INT目标,常量状态和放大器状态){
如果(目标℃下)返回; //假设我们不允许任何底片
如果(IP ==端和放大器;&安培;目标== 0){
state.dump();
返回;
}
如果(IP ==完)
返回;
{//没有第一个
SS(IP + 1,最后,目标,状态);
}
{//与第一个
INT第一= * IP;
SS(IP + 1,最后,靶 - 第一,国家(第一,状态));
}
}
诠释的main(){
诠释一个[] = {2,59,3,43,5,9,8,62,10,4};
INT *开始=&放大器;一个[0];
INT *结束=启动+的sizeof(A)/的sizeof(A [0]);
SS(开始,结束,12,州());
}
My goal here is to find all possible combinations that sums to a given total. For example, if the array is 2 59 3 43 5 9 8 62 10 4 and if the total is 12, then possible combinations are
2 10
3 9
8 4
5 3 4
Here is the first set of code, that I've written. Wondering the best improvements that can be done on this.
int find_numbers_matching_sum(int *number_coll, int total)
{
int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total);
int location = search_till - number_coll;
if (*search_till > total && location > 0 )
{
--location;
}
while ( location >= 0 )
{
find_totals(number_coll,total,location);
--location;
}
return 1;
}
int find_totals(int *number_coll, int total, int end_location)
{
int left_ones = total - number_coll[end_location];
int curloc = end_location;
int *search_till = 0;
int location ;
int all_numbers[10];
int i = 0;
all_numbers[i] = number_coll[end_location];
while ( left_ones && curloc >= 0 )
{
search_till = lower_bound(number_coll,number_coll+end_location, left_ones);
location = search_till - number_coll;
if (*search_till > left_ones && location > 0 )
{
--location;
}
else if ( left_ones < *search_till )
{
break;
}
curloc=location;
left_ones = left_ones - number_coll[curloc];
all_numbers[++i] = number_coll[curloc];
end_location = curloc - 1;
}
if ( !left_ones )
{
while ( i>=0)
{
cout << all_numbers[i--] << ' ';
}
}
cout << endl;
return 1;
}
解决方案
#include <iostream>
#include <vector>
using namespace std;
struct State {
int v;
const State *rest;
void dump() const {
if(rest) {
cout << ' ' << v;
rest->dump();
} else {
cout << endl;
}
}
State() : v(0), rest(0) {}
State(int _v, const State &_rest) : v(_v), rest(&_rest) {}
};
void ss(int *ip, int *end, int target, const State &state) {
if(target < 0) return; // assuming we don't allow any negatives
if(ip==end && target==0) {
state.dump();
return;
}
if(ip==end)
return;
{ // without the first one
ss(ip+1, end, target, state);
}
{ // with the first one
int first = *ip;
ss(ip+1, end, target-first, State(first, state));
}
}
int main() {
int a[] = { 2,59,3,43,5,9,8,62,10,4 };
int * start = &a[0];
int * end = start + sizeof(a) / sizeof(a[0]);
ss(start, end, 12, State());
}