兑换硬币
把一元纸币换成一分、二分、五分硬币(每种至少一枚),有多少种换法?
//DFS
#include<iostream> using namespace std; #define LEN 3 int a[] = {1, 2, 5}; int remain(int i){ int total = 0; for(int j=LEN-1;j>i;j--) total += a[j]; return total; } long dfs(int total, int i){ if(i>=3){ if(total==0) return 1; return 0; } long res = 0; for(int num=1;total-a[i]*num>=remain(i);++num){ res += dfs(total-num*a[i], i+1); } return res; } int main(){ printf("%ld\n", dfs(100, 0)); return 0; }