zoj 2734 Exchange Cards
2015-08-05 20:23
405 查看
Exchange Cards
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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
Sample Output
思路:给定要兑换的总价值和卡片的种类,然后给出每种卡片的价值和数量,求有多少种兑换成总价值的方式
代码:
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 2 2 1 3 1 10 5 10 2 7 2 5 3 2 2 1 5
Sample Output
1 7
思路:给定要兑换的总价值和卡片的种类,然后给出每种卡片的价值和数量,求有多少种兑换成总价值的方式
代码:
#include<stdio.h> int v,n,ans,sum; int v1[1100],n1[20]; void dfs(int x) { if(sum==v) { ans++; return ; } if(sum>v||x>n) return ; dfs(x+1); for(int i=1;i<=n1[x];i++) { sum+=v1[x]; dfs(x+1); } for(int i=1;i<=n1[x];i++) sum-=v1[x]; } int main() { int s=0; int i; while(scanf("%d%d",&v,&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%d%d",&v1[i],&n1[i]); } sum=0; ans=0; dfs(1); printf(s!=0?"\n%d\n":"%d\n",ans); s++; } return 0; }
相关文章推荐
- Android studio 取消language injection
- nginx 进行外网地址屏蔽
- 【华为oj】提取不重复的整数
- xml实现rss订阅
- C#操作SQLite数据库帮助类——SQLiteHelper
- 电容标识
- hdoj 1379 DNA Sorting【排序】
- 新浪微博简记
- php调用SQL SERVER 2008及以上版本的方法
- 读书笔记--大话设计模式
- ESP定律和堆栈平衡
- 设计模式之简单工厂模式;
- tomcat配置虚拟目录映射
- Yii 1.x IOS推送
- ANDROID 获取时间格式
- CC攻击原理学习笔记
- Linux 信号理解(二)
- 如何添加一个已有项目到GitHub
- JDBC 2:操作Dept表
- getRequestDispatcher()与sendRedirect()的区别