回溯法求一個集合中和為定值的所有集合

惠hiuj發表於2024-05-07
/********************************************************************************************************
*
*	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;
}

相關文章