博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
递归找零问题
阅读量:7171 次
发布时间:2019-06-29

本文共 618 字,大约阅读时间需要 2 分钟。

题目:现有1元、5元、10元、20元、50元面值不等的钞票,问需要90元钱有多少种找钱方案

此题如果没要求打印结果,只要求打印出一共有多少种方案,那么可以采用贪心算法

首先选取最大的,然后逐次选择最小的进行递归实现

代码如下:

#include "stdafx.h"

#include <iostream>
#include <stdio.h>
using namespace std;
int arr[5]={50,20,10,5,1};
int fun( int n,int index) {
if(n<0) return 0;
if(n==0)return 1;
int num=0;
if(index==5) return 0;
for(int i=0;i<=n/arr[index];i++)
{
num+=fun(n-i*arr[index],index+1);
}
return num;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<fun(1,0)<<endl;
cout<<fun(5,0)<<endl;
cout<<fun(10,0)<<endl;

cout<<fun(90,0)<<endl;

system("pause");

return 0;
}

转载于:https://www.cnblogs.com/fjsh925907195/p/4138846.html

你可能感兴趣的文章
oo第一次博客
查看>>
maven3简单配置和使用
查看>>
广工大专用教学质量评价脚本
查看>>
不同的 SQL JOIN
查看>>
SQL Date函数
查看>>
牛客小白月赛4 B 博弈论 思维 字符串
查看>>
一个用bootstrap写的小页面
查看>>
钉钉服务端api接口使用
查看>>
POJ 1045:Bode Plot
查看>>
java基础总结之Hashtable
查看>>
c++11 记录
查看>>
自定义简单的模板引擎-JS模板引擎
查看>>
c#事件机制
查看>>
JS 得细心的坑位
查看>>
【转】基于laravel制作APP接口(API)
查看>>
C#GridViewExport帮助类,美化导出
查看>>
今日工作情况3
查看>>
无法加载 DLL“SQLite.Interop.DLL”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。...
查看>>
Python3 os.stat() 方法
查看>>
UVA 1394 And Then There Was One 约瑟夫环数学方法
查看>>