标签:exchange cards zoj 2734 zoj 搜索
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
dfs即可:
#include<stdio.h> #include<string.h> int val,n,ans,sum; int num[1010]; void dfs(int x) { int i; if(sum==val)//达到价值ans++ { ans++; return ; } for(i=x;i<=val;i++) { if(num[i]&&sum+i<=val)//i价值的卡片有剩余 ,且没有达到val的大小 { num[i]--; sum+=i; dfs(i); sum-=i; num[i]++; } } } int main() { int bl=0; while(scanf("%d%d",&val,&n)!=EOF) { if(bl) printf("\n"); bl++; int x,y; ans=sum=0; memset(num,0,sizeof(num)); while(n--) { scanf("%d%d",&x,&y); num[x]=y; } dfs(1); printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:exchange cards zoj 2734 zoj 搜索
原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47813923