banknotesandcoins1021Banknotes and Coins beeCrowd 1021 URI 1021 Solution C Python programming solved solving easy neilor tonin brazil money input 1e-9
Banknotes and Coins
This problem is from beeCrowd. Here, You get the amount of money as input. It is an integer. You have to calculate and print the least amount of banknotes and coins required to cover that much money. Banknotes and coins, as given, are 100,50,20,10,5,2 and 1, 0.50, 0.25, 0.10, 0.05, 0.01.
The formula is:
- Take input.
- Create a list for currency and one for currencyCount.
- Print "NOTAS:"
- Start a while loop checking for each note in currency.
- Run a while loop that checks if that particular note is >= inputted value.
- Add 1 to currencyCount lists the same index as the index of that note or coin. Subtract notes amount from money.
- Check if currency == 1 then, print "MOEDAS:". This way "MOEDAS:" will be printed only once.
- check if currency >=2 or <2. If currency >=2 print notes and if currency <2 print coins as said in beecrowd.
The solution in C:
#include <stdio.h>
int main() {
double money;
scanf("%lf",&money);
double currency[12] = {100,50,20,10,5,2,1, 0.50, 0.25, 0.10, 0.05, 0.01};
int currencyCount;
printf("NOTAS:\n");
money+=1e-9;
for(int i = 0; i<=11;i++){
currencyCount = 0;
while(money>=currency[i]){
currencyCount+=1;
money-=currency[i];
}
if(currency[i]==1)
printf("MOEDAS:\n");
if(currency[i]>1){
printf("%d nota(s) de R$ %0.2f\n",currencyCount,currency[i]);
}
if(currency[i]<2){
printf("%d moeda(s) de R$ %0.2f\n",currencyCount,currency[i]);
}
}
return 0;
}
The solution in Python:
money = float(input())
currency = [100,50,20,10,5,2,1, 0.50, 0.25, 0.10, 0.05, 0.01]
print("NOTAS:");
i = 0
money+=1e-9;
while i<=11:
currencyCount = 0
while money>=currency[i]:
currencyCount+=1
money-=currency[i]
if currency[i]==1:
print("MOEDAS:")
if currency[i]>1:
print("%d nota(s) de R$ %0.2f" % (currencyCount,currency[i]))
if currency[i]<2:
print("%d moeda(s) de R$ %0.2f" % (currencyCount,currency[i]))
i+=1
COMMENTS