#include
int set(int * array, int arraySize, int sum, int * ans, int ansSize){
int i;
if (arraySize == 0){
if (sum != 0) return 0;
printf("%d",ans[0]);
for (i=1; i
else printf("+%d", ans[i]);
}
printf(";\n");
return 1;
}
i= set(array+1, arraySize-1, sum, ans, ansSize);
ans[ansSize++] = array[0];
return i+set(array+1, arraySize-1, sum-array[0], ans, ansSize);
}
int main(){
int array[]={-10, 45, 35, 99, 10, 6, 9, 20, 17, 18};
int ans[20];
int arraySize = 10;
int size=0;
int sum = 35;
int num;
num = set(array, arraySize, sum, ans, size);
}
子集和问题,如果是整数的话而且和不大,考虑DP.
否则就搜吧..
明天给你答案
发消息给你了