Program Fractional Knapsack
#include <cstdlib>
#include <iostream>
using namespace std;
void SolveFract(int p[], int w[], float W, int n, int x[], float &TotalUntung){
int i;
float kapasitas;
bool MasihMuatUtuh;
for(i=0;i<n;i++){
x[i];
}
kapasitas = W;
TotalUntung = 0;
i = 0;
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[] = {9,8,6,10};
int p[] = {20,12,10,7};
float W = 17;
int n = 4;
int x[] = {};
float TotalUntung = 0;
SolveFract(p, w, W, n, x, TotalUntung);
cout << TotalUntung << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
void SolveFract(int p[], int w[], float W, int n, int x[], float &TotalUntung){
int i;
float kapasitas;
bool MasihMuatUtuh;
for(i=0;i<n;i++){
x[i];
}
kapasitas = W;
TotalUntung = 0;
i = 0;
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[] = {9,8,6,10};
int p[] = {20,12,10,7};
float W = 17;
int n = 4;
int x[] = {};
float TotalUntung = 0;
SolveFract(p, w, W, n, x, TotalUntung);
cout << TotalUntung << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Komentar
Posting Komentar