Fractional Knapsack 1

#include <cstdlib>
#include <iostream>

using namespace std;
void SolveFractionalKnapsack(int p[], int w[], float W, int n, int x[], float TotalUntung){
     int i;
     float kapasitas;
     bool MasihMuatUtuh;
    
     for(i=1;i>n;i++){
                      x[i];
                      }
     kapasitas = W;
     TotalUntung = 0;
     i = 1;
     MasihMuatUtuh = true;
     while ((i <= n) && (MasihMuatUtuh)){
           if (w[i] <= kapasitas){
                    x[i]=1;
                    TotalUntung = TotalUntung + p[i];
                    kapasitas = kapasitas -w[i];
                    i = i + 1;
                    }
           else {
                MasihMuatUtuh = false;
                }
           }
     if(i <= n ){
          x[i] = kapasitas / w[i];
          TotalUntung = TotalUntung + x[i]*p[i];
          }
     }
    
int main(int argc, char *argv[])
{
    int w[] = {5,8,6,10};
    int p[] = {10,12,7,20};
    float W = 17;
    int n = 4;
    int x[] = {};
    float totaluntung = 0;
   
    SolveFractionalKnapsack(p, w, W, n, x, TotalUntung);
   
    cout << totaluntung << endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

Komentar