4 ms·
My solver found 351 solutions without repeating items. But the article says there are 279. What is wrong? #include <stdio.h> #define N_ITEMS 21 int
by marcodiego 5y ago
My solver found 351 solutions without repeating items. But the article says there are 279. What is wrong?
#include <stdio.h>
#define N_ITEMS 21
int prices[] = {122,275,185,597,647,216,713,457,146,518,316,489,711,
645,477,804,671,231,621,98,87};
int sol[N_ITEMS] = {0};
void show_sol(void) {
printf("[");
for(int i = 0; i < N_ITEMS; i++)
printf("%d%c", sol[i], i != N_ITEMS - 1 ? ',' : '\0');
printf("]\n");
}
void solve(int limit, int depth)
{
if(depth == N_ITEMS)
return;
if(limit == 0){
show_sol();
return;
}
solve(limit, depth+1);
sol[depth]++;
limit -= prices[depth];
solve(limit, depth+1);
sol[depth] = 0;
}
int main(void) {
solve(4394, 0);
return 0;
}
- lmns 5y agoThe author forgot to add the "duckie" item in the list.
- abrax3141 5y agoThe OP wrote in Lisp. The Lisp compiler reflects on the code, and realizing that it’s trying all combinations, cross-compiled it to a quantum computer which produced an approximate answer.
- mkl 5y agoThere are actually 686 solutions. The problem is you're exiting the solve function before showing the ones that include the last price. Swapping the order of the two if statements fixes it.
- marcodiego 5y agoThanks!
- Tempest1981 5y agoNote that this line only initializes element 0, not all elements: int sol[N_ITEMS] = {0}; Did you mean {}?