Advertisement
sifat3d

BASIC Greedy Algorithm C

Jun 9th, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. //1,2,5,10,20,50
  3. int main()
  4. {
  5.  
  6. int amount;
  7. int fiftytk=0,twentytk=0,tentk=0,fivetk=0,twotk=0,onetk=0;
  8.     printf("Enter your Taka for CHANGE:\t");
  9.     scanf("%d",&amount);
  10. int original_am=amount;
  11.  
  12. //fiftycheck
  13. while(amount>=50)
  14.     {
  15.         amount=amount-50;
  16.         fiftytk++;
  17.     }
  18. //twentycheck
  19. while(amount>=20)
  20.     {
  21.         amount=amount-20;
  22.         twentytk++;
  23.     }
  24. //tencheck
  25. while(amount>=10)
  26.     {
  27.         amount=amount-10;
  28.         tentk++;
  29.     }
  30. //fivecheck
  31. while(amount>=5)
  32.     {
  33.         amount=amount-5;
  34.         fivetk++;
  35.     }
  36. //twocheck
  37. while(amount>=2)
  38.     {
  39.         amount=amount-2;
  40.         twotk++;
  41.     }
  42. //FINALLY onecheck
  43. while(amount>=1)
  44.     {
  45.         amount=amount-1;
  46.         onetk++;
  47.     }
  48.  
  49. if(amount==0)
  50.     {
  51.         printf("For %d Greedy is \nfiftytk=%d,\ntwentytk=%d,\ntentk=%d,\nfivetk=%d,\ntwotk=%d,\nonetk=%d;",original_am,fiftytk,twentytk,tentk,fivetk,twotk,onetk);
  52.     }
  53.     else printf("Greedy FAILED %d",amount);
  54.  
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement