listing METODE GREEDY..mata kuliah PAA

22.18 Edit This 0 Comments »
LISTING PROGRAM

#include
#include
#define size 99

void sort (int[],int);
main(){
            clrscr();
            int x[size],i,n,uang,hasil[size];
            printf("\nBanyak Jenis Koin : ");
            scanf("%d",&n);
            printf("\nMasukkan Jenis Koin: \n");
            for (i=1;i<=n;i++)
            {
                        scanf("%d",&x[i]);
            }
                        sort (x,n);
                        printf("\nJenis Koin Yang Tersedia : " );
                        for (i=1;i<=n;i++)
                        {
                                    printf("%d\t",x[i]);
                        }
            printf("\nMasukkan Nilai Yang Ingin Dipecah: ");
            scanf("%d",&uang);
            printf("\nHasil Algoritma Greedy-nya adalah :");
            for (i=1;i<=n;i++)
            {
                        hasil[i]=uang/x[i];
                        uang=uang%x[i];
            }
            for (i=1;i<=n;i++)
            {
                        printf("\nKeping %d",x[i]);
                        printf("-an sebanyak: %d keping",hasil[i]);
                        printf("\n");
            }
            printf("\n");
            getch();
            return 0;
}
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]
                                    {
                                                hold=a[j+1];
                                                a[j+1]=a[j+2];
                                                a[j+2]=hold;
                                    }
                        }
            }
}

output






0 komentar: