您的位置:首页 > 其它

HDU_2069 Coin Change(dfs)

2015-06-27 13:54 411 查看
题目请点我

题解:

钱币的问题,背包应该是能做的,但是用暴力dfs也过了。注意所有的钱币加起来不能超过100枚,被这里坑了,睡了一觉醒来把它A掉了。

代码实现:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define MAX_C 110
#define MAX_M 255
#define LL long long

using namespace std;

int result;
int money;
int type[5] = {50,25,10,5,1};
int dfs(int num,int t,int sum);
int main()
{
while( scanf("%d",&money) != EOF ){
result = dfs(money,0,0);
printf("%d\n",result);
}
return 0;
}

int dfs(int num,int t,int sum){
if( t == 4 ){
if( sum+num <= 100 ){
return 1;
}
else{
return 0;
}
}
int left = num;
int tmp = 0;
int times = sum;
while( left >= 0 && times <= 100 ){
tmp += dfs(left,t+1,times);
left -= type[t];
times++;
}
return tmp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs