假設顧客給了一筆小現金,那這筆現金可以換成四種硬幣
分別為50$、10$、5$、1$,以最少硬幣數量換取
可換成各多少個硬幣數量並且把每個硬幣數量加總起來.
#include<stdio.h>
#include<stdlib.h>
int main(){
int coin1=50,coin2=10,coin3=5,coin4=1;
int change,pay,change1,change2,change3,change4;
printf("Enter a cash:");
scanf("%d",&pay);
change=pay/coin1;
int ch0=change;
printf("a amount of coin50 is %d",change);
printf("\n");
change=(pay%coin1)/coin2;
int ch1=change;
printf("a amount of coin10 %d",change);
printf("\n");
change=(pay%coin2)/coin3;
int ch2=change;
printf("a amount of coin5 %d",change);
printf("\n");
change=(pay%coin3)/coin4;
int ch3=change;
printf("a amount of coin1 %d",change);
printf("\n");
printf("mounts of total coin is %d",ch0+ch1+ch2+ch3);
return 0;
--------------------------------
其實寫的有點冗長,可以把printf刪減許多,不過因為是練習,所以多打了幾行printf
而運算式也是獨立打出來,並沒有把所有運算式寫在printf裡.
留言列表