刷题记录:牛客NC22197零钱兑换

传送门:牛客

n元人民币换成1元、2元、5元的零钱,请计算共有多少种兑换方法?
输入:
100
输出:
541

做这道的第一想法是用递归求解的方式但是n有200而已,想了一想完全不需要递归(递归甚至更加麻烦,直接暴力即可求解)
直接枚举1元,2元,5元的分别的个数即可,中途可以适当的优化

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
	ll x=0,w=1;char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*w;
}
#define maxn 1000000
int n;
int main() {
	n=read();int sum=0;
	for(int i=0;i<=n/5;i++) {
		for(int j=0;j<=(n-5*i)/2;j++) {
			for(int k=0;k<=(n-5*i-2*j);k++) {
				if(5*i+2*j+k==n) {
					sum++;
				}
			}
		}
	}
	cout<<sum<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值