Anonymous user
Knapsack problem/Unbounded: Difference between revisions
→{{header|C}}
Line 183:
{{trans|Fortran}}
<c>#include <stdio.h>
double min(double a, double b) {
return a < b ? a : b;
Line 190:
struct Bounty {
int value;
double weight, volume;
};
Line 198 ⟶ 197:
gold = {2500, 2.0, 0.002},
sack = { 0, 25.0, 0.25 },
current, best;
#define CALC(V) current.V =
int main() {▼
int n[3];▼
▲int main(void) {
max_panacea = min(sack.weight / panacea.weight, sack.volume / panacea.volume);▼
int npanacea, nichor, ngold, max_panacea, max_ichor, max_gold;
max_ichor = min(sack.weight / ichor.weight, sack.volume / ichor.volume);▼
▲ max_panacea = (int)min(sack.weight / panacea.weight, sack.volume / panacea.volume);
for (j = 0; j <= max_ichor; j++) {▼
▲ max_ichor = (int)min(sack.weight / ichor.weight, sack.volume / ichor.volume);
max_gold = (int)min(sack.weight / gold.weight, sack.volume
CALC(weight);▼
for (npanacea = 0; npanacea <= max_panacea; npanacea++) {
for (ngold = 0; ngold <= max_gold; ngold++)
current.weight = npanacea *
current.volume = npanacea *
if (current.value > best.value
best.volume = current.volume;
best_amounts[0] = npanacea;
best_amounts[1] = nichor;
best_amounts[2] = ngold;
}
}
Line 233 ⟶ 230:
}
printf("Maximum value achievable is %d\n", best.value);
best_amounts[0], best_amounts[1], best_amounts[2]);
return 0;
}</c>
|