Ich habe ein Problem mit meinem Rucksack-Algorithmus. Um ehrlich zu sein, ich habe keine Ahnung, was falsch ist. Wenn ich Programm einmal benutze, funktioniert alles falsch, aber wenn ich mein Programm in Schleife (für Test) verwende, habe ich ein großes Problem.C++ Rucksack Implementierung
Zum Beispiel:
Gewicht/Val in der Datei: 100
max Kapazität Ranzen: 1000
Erste Iteration:
Max Gewinn: 2597
Das resultierende Gewicht: 994/1000
Und seine feinen , aber jetzt eine weitere Iteration.
Zweite Iteration:
Max Gewinn: 2538
Das resultierende Gewicht: 1004/1000 < - und es ist mein Problem, sein über meine max Kappe.
3., 4. war okey, dann 5. war falsch (1355/1000), und so weiter.
Meine Funktion ist, wo möglich Problem:
void intoKnapsack(int k, float actual_profit, float actual_weight)
{
if (actual_weight + weight[k] <= cap)
{
tmp[k] = 1;
if (k <= number_items)
intoKnapsack(k + 1, actual_profit + value[k], actual_weight + weight[k]);
if (((actual_profit + value[k]) > final_profit) && (k == number_items))
{
final_profit = actual_profit + value[k];
final_weight = actual_weight + weight[k];
for (j = 0; j <= k; j++)
knap[j] = tmp[j];
}
}
else if ((bound(actual_profit, actual_weight, k) >= final_profit))
{
tmp[k] = 0;
if (k <= number_items)
intoKnapsack(k + 1, actual_profit, actual_weight);
if ((actual_profit > final_profit) && (k == number_items))
{
final_profit = actual_profit;
final_weight = actual_weight;
for (j = 0; j <= k; j++)
knap[j] = tmp[j];
}
}
}
Kann jemand bei meinem Problem helfen?
Jede Chance, dass Sie die Politur Code übersetzen könnte? Sonst können wir es nicht lesen. –
Erledigt: D Jetzt sollte es einfacher sein – Slideroh
Grobe Übersetzung: http://pastebin.com/xVZXFDp4 (Danke, Google Übersetzer lol). ** Edit **: Ah, er übersetzte es selbst etwa 30 Sekunden vor dem Übersetzen Übersetzen über Google Translate xD – SpencerD