博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【练习赛2补题】zoj 2734 Exchange Cards 【DFS】
阅读量:5052 次
发布时间:2019-06-12

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

As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his friends for cards he likes. Of course, different cards have different value, and Mike must use cards he owns to get the new one. For example, to get a card of value 10$, he can use two 5$ cards or three 3$ cards plus one 1$ card, depending on the kinds of cards he have and the number of each kind of card. And Sometimes he will involve unfortunately in a bad condition that he has not got the exact value of the card he is looking for (fans always exchange cards for equivalent value).

Here comes the problem, given the card value he plans to get and the cards he has, Mike wants to fix how many ways he can get it. So it's you task to write a program to figure it out.

Input

The problem consists of multiple test cases, terminated by EOF. There's a blank line between two inputs.

The first line of each test case gives n, the value of the card Mike plans to get and m, the number of different kinds of cards Mike has. n will be an integer number between 1 and 1000. m will be an integer number between 1 and 10.

The next m lines give the information of different kinds of cards Mike have. Each line contains two integers, val and num, representing the value of this kind of card, and the number of this kind of card Mike have.

Note: different kinds of cards will have different value, each val and num will be an integer greater than zero.

Output

For each test case, output in one line the number of different ways Mike could exchange for the card he wants. You can be sure that the output will fall into an integer value.

Output a blank line between two test cases.

Sample Input

5 22 13 110 510 27 25 32 21 5

Sample Output

17 题意:输入价值总v及行数m,每行输入两个整数,价值si和该价值的卡片数目ni,问,各价值组合得到总价值的组合方式有多少种? 思路:只要题意get到就是一个dfs,注意,两个样例之间有空行
#include
#include
#define N 1010int num[N];int v,m,sum,ans;void dfs(int k){ int i; if(sum == v) { ans++; return; } for(i = k; i <= v; i ++) { if(sum + i <= v&&num[i] > 0) { num[i]--; sum += i; dfs(i); num[i]++; sum -= i; } } return;}int main(){ int t1,t2,i; int t = 0; while(scanf("%d%d",&v,&m)!=EOF) { ans = sum = 0; if(t) printf("\n"); memset(num,0,sizeof(num)); for( i = 1; i <= m; i ++) { scanf("%d%d",&t1,&t2); num[t1] = t2; } dfs(1); printf("%d\n",ans); t++; } return 0;}

 

 

 

转载于:https://www.cnblogs.com/hellocheng/p/7390861.html

你可能感兴趣的文章
yii2框架dropDownList的下拉菜单用法介绍
查看>>
c#截取两个指定字符串中间的字符串
查看>>
UDP基础-1
查看>>
康托展开了解一下
查看>>
通讯聊天工具(pingin)
查看>>
odoo10 高级视图
查看>>
IE 专有的事件驱动方法 Named Script
查看>>
hibernateTemplate.find或hibernateTemplate.save()执行操作没有反应,但是有sql语句
查看>>
SQL Server 查询性能优化——索引与SARG(一)
查看>>
到底还是中国人,这官话都一套一套的
查看>>
团队冲刺07
查看>>
自学知识(四)
查看>>
字典操作
查看>>
Java 排序(快排,归并)
查看>>
解析html
查看>>
linux日常管理-系统进程查看工具-ps
查看>>
HandlerThread&Looper&MessageQueue
查看>>
ROM的一种写法
查看>>
VIM Taglist + ctags
查看>>
supervisord
查看>>