【发布时间】:2019-09-12 07:35:16
【问题描述】:
当我用大于 12 个元素的大型向量测试我的程序时,它会崩溃(我收到 lldb 错误)。但是,它适用于小向量。我认为它正在尝试访问它不应该访问的内存空间,但我不知道如何修复它。 该程序应该打印出元素总和等于“目标”的向量 另外,有没有不同的表达方式: if (i & (1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int c = 0;
/* find subsets of a given set */
void findSubsets(int *value, int n, int i) {
int j;
if (i < 0)
return;
for (j = 0; j < n; j++) {
/*
* checking jth bit is set in i. If
* it is set, then fetch the element at
* jth index in value array
*/
if (i & (1 << j)) {
suma = suma + value[j];
}
/* recursive call */
findSubsets(value, n, i - 1);
return;
}
int main() {
/* 2^n - indicates the possible no of subsets */
int count = pow(2, size);
/* finds the subsets of the given set */
findSubsets(vector, size, count - 1);
return 0;
}
我希望能够将此程序用于大型向量(最多约 20 个)
【问题讨论】: