coinmain.cpp
来自「经典的硬币问题:使用最少个数的硬币来达到指定的钱数。使用贪心算法。vc下调试通过」· C++ 代码 · 共 40 行
CPP
40 行
#include <stdio.h>
#include "SortFun.h"
int main()
{
int i;
double money; /*the money need to be changed */
double lastmoney; /* when change happen,the money last*/
double coin[3]={0.5,0.2,0.1}; /* the coin array */
int coincnt[3]={0,0,0}; /* the count per coin */
/********************************
We can get our own coin array without order.
Then using a bubble sort to reorder it to big-small.
Suppose dimension is N.
printf("Input your coin:\n");
for(i=0;i<N;i++)
scanf("%lf",coin[i]);
bubblesort(coin,N);
*****************************************/
printf("Please input the money you want to change!\n");
scanf("%lf",&money);
lastmoney=money;
for(i=0;i<3;i++) /* for per coin,count its number untill lowwer than lastmoney*/
{
while(lastmoney>=coin[i])
{
printf("\n%2f,%2f",lastmoney,coin[i]);
coincnt[i]++;
lastmoney-=coin[i];
}
}
for(i=0;i<3;i++)
printf("\n%lf——%d",coin[i],coincnt[i]);
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?