BackTracking_Fixed sum for array elements

本文介绍了一种使用递归回溯算法解决固定数值组合求和问题的方法。通过列举所有可能的子集来找出那些元素之和等于指定数值的组合。文中提供了三种实现方式:一种基于类的递归回溯解决方案,另一种采用递归函数实现,最后还提供了一个C语言版本的实现。

 

Given an array a contains distinct positive integers, count how many combinations of integers in a add up to exactly sum

For example, given int[] a = {11, 3, 8} ; and sum = 11

You should output 2, because 11 == 11 and 3 + 8 == 11

This is typically a backtracking problem

Enumerate all the subsets of the given array to see how many of them match the condition

when you write backtracking procedure using recursion, please be careful of which condition do you

use to terminate the loop, in this code snippet, there two conditions,

1. sum == 0

2. t == a.Length

and when t == a.Length, we might be got an solution yet, don't forget this case.

Backtracking

Code
 1 class FixedSum
 2 {
 3     public int Count(int[] a, int sum)
 4     {
 5         Backtrack(a, 0, sum);
 6         return count;
 7     }
 8 
 9     // t is the index used to control the recursive procedure
10     public void Backtrack(int[] a, int t, int sum)
11     {
12         if (t == a.Length)
13         {
14             // although t is out of range, but we got a solution
15             // that means the last element of a is part of solution
16             if (sum == 0)
17                 count++;
18             return;
19         }
20 
21         if (sum == 0// got a solution
22             count++;
23         else
24         {
25             if (sum - a[t] >= 0)
26                 Backtrack(a, t + 1, sum - a[t]);// enter left tree
27 
28             if (sum >= 0)
29                 Backtrack(a, t + 1, sum); // enter right tree
30         }
31     }
32 
33     // Count how many solutions there is
34     private int count = 0;
35 
36 }
37 

recursive way

Code
// i is the index, n is sum
public int Combo(int[] a, int i, int n)
{
    
if (n == 0// sum is 0, then no element needed, so it's an empty solution
        return 1;
    
if (i < 0// index < 0, no solution found
        return 0;
    
if (a[i] > n) // exclude the element greater than n
        return Combo(a, i - 1, n);
    
else // else compute the solution include n plus exclude n.
        return Combo(a, i - 1, n) + Combo(a, i - 1, n - a[i]);
}

C法

#include<stdio.h>
#include<stdlib.h>

int count = 0; // number of solutions

/*
 * array - positive numbers
 * n     - element count in array
 * sum   - pair of sum
 * t     - recursion deep
 */
void find_combinations(int *array, int n, int sum, int t) {
    if (t == n) {
        if (sum == 0) {
            count++;
        }
        return;
    }

    if (sum == 0) { // Find a solution
        count++;
    }
    else {
        if (sum >= array[t]) {  // left tree
            find_combinations(array, n, sum - array[t], t + 1);
        }
        if (sum > 0) {                  // right tree
            find_combinations(array, n, sum, t + 1);
        }
    }
}

int main(void) {
    int a[] = {11, 3, 8, 4, 1, 7};
    find_combinations(a, 6, 11, 0);
    printf("%d\n", count);

    system("pause");
    return 0;
}

==

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值