0% found this document useful (0 votes)
47 views

Tugas Kelompok: Kelas: ANALGO 9

The document discusses three algorithms: (1) coin change problem that uses a greedy approach to provide the optimal number of coins for a given amount of money, (2) scheduling problem that sorts customers by arrival time, (3) knapsack problem that uses dynamic programming to find the optimal solution to fit items in a knapsack. The code includes classes and functions to get input, calculate solutions, and display outputs for each algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Tugas Kelompok: Kelas: ANALGO 9

The document discusses three algorithms: (1) coin change problem that uses a greedy approach to provide the optimal number of coins for a given amount of money, (2) scheduling problem that sorts customers by arrival time, (3) knapsack problem that uses dynamic programming to find the optimal solution to fit items in a knapsack. The code includes classes and functions to get input, calculate solutions, and display outputs for each algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Tugas kelompok

Firda Alfiyani W

10111262

Andri Muhammad Julianto

10111257

Chendy Arina

10111603

Kelas

: ANALGO 9

#include <cstdlib>
#include <iostream>
#define ukuran 99

using namespace std;

void sort(int[],int);

class knap

int n,p[50],w[50],c,oldc,tp,tw;

int i,y,x[50],f[50][120];

public:

void get();

void findf();

void findopt();

void display();

void hitung();

};

void knap::hitung()

tp=0;tw=0;
for(int i=1;i<=n;i++)

{
if(x[i]==1){
tw+=w[i];
tp+=p[i];
}
}

cout<<"\n\n\t Nilai Weight TB Optimal : \t"<<tw<<endl;


cout<<"\n\n\t Nilai Profit TP Optimal : \t"<<tp<<endl;

void knap::get()

cout<<"\n\n\t Masukan Banyak N :\t";

cin>>n;

cout<<"\n\n\t Masukan CAPACITY dari KNAPSACK:\t";

cin>>c;

oldc=c;

for(int i=1;i<=n;i++)

cout<<"\n\n\n\t Masukan PROFIT ITEM ke "<<i<<":\t";

cin>>p[i];

cout<<"\n\t Masukan WEIGHT ITEM ke "<<i<<":\t";

cin>>w[i];

void knap::findf()

for(y=0;y<w[n];y++)

f[n][y]=0;

for(y=w[n];y<=c;y++)

f[n][y]=p[n];

for(int i=n-1;i>1;i--)

for(int y=0;y<w[i];y++)

f[i][y]=f[i+1][y];

for(y=w[i];y<=c;y++)

f[i][y]=(f[i+1][y]>(f[i+1][y-w[i]]+p[i]))?f[i+1][y]:(f[i+1][y-w[i]]+p[i]);

f[1][c]=f[2][c];

if(c>=w[1])

f[1][c]=(f[1][c]>f[2][c-w[1]]+p[1])?f[1][c]:f[2][c-w[1]]+p[1];

void knap ::findopt()

findf();

for(i=1;i<n;i++)

if(f[i][c]==f[i+1][c])

x[i]=0;

else

x[i]=1;

c-=w[i];

x[n]=(f[n][c])?1:0;

void knap::display()

cout<<"\n\n\t Banyak ITEMS:"<<n<<endl;

cout<<"\n\n\t CAPACITY KNAPSACK :"<<oldc<<endl;

cout<<"\n\n\t Nilai Optimalnya dalah:"<<endl;

for(int i=1;i<=n;i++)

cout<<"\n\t"<<"x["<<i<<"]:"<<x[i]<<" "<<endl;

struct pelanggan{
int id, t;
};

void PenjadwalanPelanggan(int n, pelanggan p[]) {

int i, temp;

for(i=0; i<n; i++) {


cout<< " Masukan Waktu pelanggan " << i+1 << " : ";
p[i].id=i+1;
cin>>p[i].t;

for(i=0; i<n; i++) {


for(int x=0; x<n; x++) {
if(p[x].t>p[x+1].t) {
temp=p[x].t;
p[x].t=p[x+1].t;
p[x+1].t=temp;

temp=p[x].id;
p[x].id=p[x+1].id;
p[x+1].id=temp;
}
}
}

cout<<endl;

cout<<" Urutan Yang dilayani : "<<endl;


for(i=0; i<n; i++) {
cout<<" Pelangan "<<p[i].id;
cout<<endl;
}

cout<<endl<<endl;

int main(int argc, char *argv[])


{
int menu,x;
cout<< "--------------------- MENU -------------------\n";
cout<< "\n";
cout<< "1. Penukaran uang koin\n";
cout<< "2. Minimasi waktu di dalam sistem (Penjadwalan)\n";
cout<< "3. Persoalan Knapsack\n";
cout<< "\n";
cout<< "-----------------------------------------------\n";
cout<< "\nMasukan menu pilihan :";cin>>menu;
cout<<"\n\n";
switch(menu){
case 1: {

cout<<"PENUKARAN UANG LOGAM"<<"\n";

int x[ukuran],i,n,uang,hasil[ukuran];
cout << "Banyaknya Koin :";cin>>n;
cout <<"Masukan Jenis Koin (Rp) : "<<"\n";

for(i=1;i<=n;i++)
{
cout<<"Koin Ke-"<<i<<"="; cin>>x[i];

}
sort(x,n);
cout<<"Koin yang tersedia: "<<"\n";

for(i=1;i<=n;i++)
{
cout<<x[i];
cout<<"\n";
}
cout<<"Masukan nilai yang ingin dipecah : Rp. ";cin>>uang;
cout<<"Hasil algoritma greedynya adalah : \n";

for(i=1;i<=n;i++)
{
hasil[i]=uang/x[i];
uang=uang%x[i];
}

for(i=1;i<=n;i++)
{
if (uang>=x[i])
{
uang=uang-x[i];
hasil[i]=hasil[i]+1;
}

else
{
i++;
}
}
for(i=1;i<=n;i++)
{
cout<<"Koin "<<x[i]<<" ";
cout<<"Sebanyak : "<<hasil[i]<<"\n";
}
break; }
case 2 : {
pelanggan y[100];

cout<<" Masukan Jumlah Pelanggan : ";


cin>>x;
PenjadwalanPelanggan(x,y);
break; }
case 3: { knap k;

cout<<"\n\n\t\t\t -----------------";

cout<<"\n\n\t\t\t KNAPSACK PROBLEM ";

cout<<"\n\n\t\t\t -----------------";

k.get();

k.findopt();

k.display();

k.hitung();
break; }
default :{ cout<<"Menu salah, Pilih 1 sampai 3\n";}
}

system("PAUSE");
return EXIT_SUCCESS;
}

void sort(int a[], int siz)


{
int pass,hold,j;
for(pass=1;pass<=siz-1;pass++)
{
for(j=0;j<=siz-2;j++)
{
if(a[j+1]< a[j+2])
{

hold=a[j+1];
a[j+1]=a[j+2];
a[j+2]=hold;
}
}

}
}

You might also like