回溯法求一个集合中和为定值的所有集合

/********************************************************************************************************
*
*	file name:	Zqh_习题.c
* 	author	 :	keyword2024@163.com
* 	date	 :	2024/05/05
* 	function   :    回溯法	
*	note	:    算法
*	
*  Copyright (c)  2023-2024   keyword2024@163.com    All right Reserved
* ******************************************************************************************************/

#include <stdio.h>
#define N 11
int w[N] = {1, 2, 7, 4, 11, 6, 5, 9, 14, 8, 3}; 
int r[N];           
int bag = 9;    

int Search(int *w, int *r, int pos, int weight); 

int main()
{
    int n = Search(w, r, N-1, bag);
    printf("%d",n);
    return 0;
}


int Search(int *w, int *r, int pos, int weight)
{
    static int sum = 0;
    int j;
   if(weight == 0){
        ++sum;
        for(j=N-1; j>pos; --j)
            if(r[j]) printf("%d ", w[j]);
   printf("\n");
    }
   else if

(weight>0 && pos>=0){
    r[pos] = 1;
    Search(w, r, pos-1, weight-w[pos]);
    r[pos] = 0;
    Search(w, r, pos-1, weight);
    }
    return sum;
}

posted @   晖_IL  阅读(10)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示