作业帮 > 综合 > 作业

c语言组合数求和 从23个数中任选几个数 和值在一定范围内

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/06/05 10:39:32
c语言组合数求和 从23个数中任选几个数 和值在一定范围内
比如90 35 40 100 30 120 180 50 100 150 80 160 140 180 .350《sum《360
输出 _+_+.+_在350 和360范围内
c语言组合数求和 从23个数中任选几个数 和值在一定范围内
我这里是用的数组{1,2,3,4,5},lz可以改成自己想要的.Don't forget to change NUM.
#include
#include
using namespace std;
const int NUM = 5; //This is the max num of array
const int TOTAL_RES = 200;//= pow(2, NUM); should be 2^NUM
bool res[TOTAL_RES][NUM] = {false};//2^5
const int array[NUM] = {1, 2, 3, 4, 5}; // You can change your array here
const int range[2] = {4, 5}; // This is the range from 4 to 5, you can change it here
bool check_range(const int index)
{
int sum = 0;
for (int i = 0; i < NUM; ++i)
if (res[index][i] == true)
sum += array[i];
if ((sum >= range[0]) && (sum