Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Last active August 29, 2015 14:23
Show Gist options
  • Save SohanChy/1e0e7eb6969a2dd14e36 to your computer and use it in GitHub Desktop.
Save SohanChy/1e0e7eb6969a2dd14e36 to your computer and use it in GitHub Desktop.
BASIC Greedy Algorithm C
#include <stdio.h>
//1,2,5,10,20,50
int main()
{
int amount;
int fiftytk=0,twentytk=0,tentk=0,fivetk=0,twotk=0,onetk=0;
printf("Enter your Taka for CHANGE:\t");
scanf("%d",&amount);
int original_am=amount;
//fiftycheck
while(amount>=50)
{
amount=amount-50;
fiftytk++;
}
//twentycheck
while(amount>=20)
{
amount=amount-20;
twentytk++;
}
//tencheck
while(amount>=10)
{
amount=amount-10;
tentk++;
}
//fivecheck
while(amount>=5)
{
amount=amount-5;
fivetk++;
}
//twocheck
while(amount>=2)
{
amount=amount-2;
twotk++;
}
//FINALLY onecheck
while(amount>=1)
{
amount=amount-1;
onetk++;
}
if(amount==0)
{
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);
}
else printf("Greedy FAILED %d",amount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment