Advertisement
juanjo12x

Lab2_Algoritmia_P2_Coin_Change

Apr 13th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*
  5.  *
  6.  */
  7. #define MIN(x,y) (((x)<(y)) ? (x) : (y))
  8. #define MAXSIZE 100
  9.  
  10. int main(int argc, char** argv) {
  11.     int F[MAXSIZE+1],D[MAXSIZE+1];
  12.     int j,m,k,n,temp;
  13.     printf("Ingrese cantidad de monedas (maximo 100):");
  14.     scanf("%d",&m);
  15.     printf("ingrese las %d monedas: ",m);
  16.     for(k=1;k<=m;k++){
  17.         scanf("%d",&temp);
  18.         D[k]=temp;
  19.     }
  20.     printf("ingrese el monto que deben sumar las monedas:");
  21.     scanf("%d",&n);
  22.     F[0]=0;
  23.     for(k=1;k<=n;k++) {
  24.         j=1;
  25.         while(j<=m && k >= D[j]) {
  26.             if(j==1) temp=F[k-D[j]];
  27.             temp= MIN(F[k-D[j]],temp);
  28.             j++;
  29.         }
  30.         F[k]=temp+1;
  31.     }
  32.     int cant[m]; // almaceno la cantidad de cada denominacion
  33.     for(j=1;j<=m;j++) cant[j]=0; // inicializo
  34.     k=n;
  35.     printf("la minima cantidad de monedas es %d \n",F[n]);
  36.     while(k>=1){ // hallo
  37.         j=1;
  38.         int jmin=1;
  39.         while(j<=m && k >= D[j]) {
  40.             if(j==1) temp=F[k-D[j]];
  41.             temp= MIN(F[k-D[j]],temp);
  42.             if((F[k-D[j]])<=temp) jmin=j;
  43.             j++;
  44.         }
  45.         cant[jmin]++;
  46.         k-=D[jmin];
  47.     }
  48.     printf("denominaciones usadas:\n");
  49.     for(k=1;k<=m;k++){
  50.         if(cant[k]>0)
  51.             printf("%d moneda(s) de %d \n",cant[k],D[k]);
  52.     }
  53.     return (EXIT_SUCCESS);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement