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
提议描述:第一个数是目标价值,第二个数是你有的种类M,后边M行是每种卡的价值及张数.求价值等于目标价值的所有组合种数;
思路:bfs便利所有组合,注意去重:
贴上两种代码:
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n,m,k,ans;
int a[100010];
int cmp(const void *a,const void *b){
return *(int *)b-*(int *)a;
}
void dfs(int pos,int sum){
if(sum==n){//等于目标价值了,计数+1,回溯
ans++;
return;
}
for(int i=pos;i<k;i++){
if(a[i]+sum<=n){//加上这一张没超,递归下一张
dfs(i+1,sum+a[i]);
while(i+1<k&&a[i]==a[i+1])//此层面额已用过,以后的等面额的可能都是重复项,循环跳过(实现去重)
i++;
}
}
}
int main(){
int flag=0;
while(scanf("%d%d",&n,&m)!=EOF){
int i;
if(flag)
printf("\n");//烦人的格式....
else
flag=1;
k=0;
for(i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
while(y--){
a[k++]=x;//此方法是将输入时将所有卡直接一张张放进数组
}
}
ans=0;
qsort(a,k,sizeof(a[0]),cmp);//按面额从大到小排序,否则函数里跳过等额那步无法执行
dfs(0,0);
printf("%d\n",ans);
}
}
第二种:更精简,思路也更清晰:
#include<stdio.h>
struct Node {
int v,num;
};
Node card[15];
int N,M,tot;
void dfs(int x,int now){
if(now==N){
tot++;return;
}if(now>N||x>=M)return;
for(int i=0;i<=card[x].num;i++){//此循环代表card[x].v这种卡取i张,递归下一种
dfs(x+1,now+i*card[x].v);
}
}
int main(){int flot=0;
while(~scanf("%d%d",&N,&M)){
if(flot++)puts("");
for(int i=0;i<M;i++)scanf("%d%d",&card[i].v,&card[i].num);//存储时放进一个结构体,价值和张数
tot=0;
dfs(0,0);
printf("%d\n",tot);
}
return 0;
}