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
static void Main(string[] args)
{
int m = 12;
string[] arr = { "1", "2", "5" };
int maxlen = m / 1;
int mixlen = m / 5;
for (int i = mixlen; i <= maxlen; i++)
{
StringCollection stringCollection = MakeStrings(arr, i);
foreach (var item in stringCollection)
{
int sum = 0;
char[] strarr = item.ToArray();
foreach (char item2 in strarr)
{
sum += Convert.ToInt32(item2 - 48);
}
if (sum == m)
{
Console.WriteLine(item);
}
}
}
Console.ReadLine();
}
static StringCollection MakeStrings(string[] characters, int finalStringLength)
{
int finalLength = finalStringLength; // The length of the final strings.
StringCollection existingStrings;
StringCollection newStrings = new StringCollection(); // Start with the all one-letter combinations.
newStrings.AddRange(characters);
for (int len = 2; len <= finalLength; len++)
{
// Start a new collection of strings based on the existing strings.
existingStrings = newStrings;
newStrings = new StringCollection();
// Concatenate every string of length (len-1)...
foreach (string str in existingStrings)
{
// ...with every character...
foreach (string ch in characters)
{
// ...to create every possible string of length len.
newStrings.Add(str + ch);
}
}
}
return newStrings;
}