BackTracking_Fixed sum for array elements
2009-06-03 16:36
344 查看
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
C法
==
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
// 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; }
==
相关文章推荐
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- The BackTracking algorithm for n queen problem
- Data Structure Array: Maximum sum such that no two elements are adjacent
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- ***(leetcode_backtracking) Combination Sum II
- 【Leetcode】Combination Sum II (Backtracking)
- Sudoku backtracking with one dimension array
- JSP for loop output all elements of an array
- General Approach for backtracking problem
- Optimization of BackTracking algorithm for n queen problem
- (M)Backtracking:39. Combination Sum
- [return sum(nums)-min(nums)*len(nums)leetcode453】 Minimum Moves to Equal Array Elements
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
- LeetCode | 453. Minimum Moves to Equal Array Elements
- LeetCode 167 Two Sum II - Input array is sorted
- 453. Minimum Moves to Equal Array Elements
- 为什么for不能有序遍历数组的所有元素?(Array的设计原理)
- look back for the "static" key word