传送门:牛客
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;
}