⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coinmain.cpp

📁 经典的硬币问题:使用最少个数的硬币来达到指定的钱数。使用贪心算法。vc下调试通过
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -