Problem Description
假设一堆由1分、2分、5分组成的n个硬币总面值为m分,求一共有多少种可能的组合方式(某种面值的硬币可以数量可以为0)。
Input
输入数据第一行有一个正整数T,表示有T组测试数据;
接下来的T行,每行有两个数n,m,n和m的含义同上。
Output
对于每组测试数据,请输出可能的组合方式数;
每组输出占一行。
Sample Input
2
3 5
4 8
Sample Output
1
2
解题思路
按照题意硬币只有面值为1,2,5的,要组成面值为m的。我们就用模拟,直接去模拟取不同面值的硬币能够组成面值为m的种数即可。
代码部分
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
using namespace std;
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
int ans=0;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)///i表示面值为1的硬币个数
{
for(int j=(m-i)/5; j>=0; j--)///j表示面值为5的硬币个数
{
if((m-i-j*5)%2==0)///如果剩余的面值还能用面值为2的硬币构成
{
if(i+j+(m-i-j*5)/2==n)///并且硬币数为n
{
ans++;
}
}
}
}
cout<<ans<<endl;
}
return 0;
}