BackTracking_Fixed sum for array elements

简介:

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

recursive way

Code

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;
}
复制代码

==

本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2009/06/03/1495466.html,如需转载请自行联系原作者

相关文章
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
212 0
LeetCode之Two Sum II - Input array is sorted
LeetCode之Two Sum II - Input array is sorted
212 0
LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
1126 0
|
移动开发
[LeetCode] Two Sum II - Input array is sorted
Problem Description: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
1152 0
|
11月前
|
测试技术 PHP 开发者
PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
|
人工智能 Java
Java 中数组Array和列表List的转换
本文介绍了数组与列表之间的相互转换方法,主要包括三部分:1)使用`Collections.addAll()`方法将数组转为列表,适用于引用类型,效率较高;2)通过`new ArrayList&lt;&gt;()`构造器结合`Arrays.asList()`实现类似功能;3)利用JDK8的`Stream`流式计算,支持基本数据类型数组的转换。此外,还详细讲解了列表转数组的方法,如借助`Stream`实现不同类型数组间的转换,并附带代码示例与执行结果,帮助读者深入理解两种数据结构的互转技巧。
1027 1
Java 中数组Array和列表List的转换
|
存储 Go 索引
go语言中的数组(Array)
go语言中的数组(Array)
370 67
|
JavaScript 前端开发 API
JavaScript中通过array.map()实现数据转换、创建派生数组、异步数据流处理、复杂API请求、DOM操作、搜索和过滤等,array.map()的使用详解(附实际应用代码)
array.map()可以用来数据转换、创建派生数组、应用函数、链式调用、异步数据流处理、复杂API请求梳理、提供DOM操作、用来搜索和过滤等,比for好用太多了,主要是写法简单,并且非常直观,并且能提升代码的可读性,也就提升了Long Term代码的可维护性。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~

热门文章

最新文章

  • 1
    PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
    290
  • 2
    Java 中数组Array和列表List的转换
    1027
  • 3
    JavaScript中通过array.map()实现数据转换、创建派生数组、异步数据流处理、复杂API请求、DOM操作、搜索和过滤等,array.map()的使用详解(附实际应用代码)
    738
  • 4
    通过array.reduce()实现数据汇总、条件筛选和映射、对象属性的扁平化、转换数据格式、聚合统计、处理树结构数据和性能优化,reduce()的使用详解(附实际应用代码)
    1567
  • 5
    通过array.some()实现权限检查、表单验证、库存管理、内容审查和数据处理;js数组元素检查的方法,some()的使用详解,array.some与array.every的区别(附实际应用代码)
    686
  • 6
    通过array.every()实现数据验证、权限检查和一致性检查;js数组元素检查的方法,every()的使用详解,array.some与array.every的区别(附实际应用代码)
    482
  • 7
    多维数组操作,不要再用遍历循环foreach了!来试试数组展平的小妙招!array.flat()用法与array.flatMap() 用法及二者差异详解
    324
  • 8
    别再用双层遍历循环来做新旧数组对比,寻找新增元素了!使用array.includes和Set来提升代码可读性
    323
  • 9
    Array.forEach实战详解:简化循环与增强代码可读性;Array.forEach怎么用;面对大量数据时怎么提高Array.forEach的性能
    206
  • 10
    深入理解 JavaScript 中的 Array.find() 方法:原理、性能优势与实用案例详解
    780