例如,输入“ 5 3”给出5作为分区数(3 + 2、3 + 1 + 1、2 + 2 + 1、2 + 1 + 1 + 1、1 + 1 + 1 + 1 + 1),不包括5或4 + 1,因为它们包含的整数大于y。
我还没有发现参数x和y与输出之间的任何关联。到目前为止,这是我设法制作的代码。它检测包含1和其他单个数字的潜在组合。到目前为止,我还没有找到一种方法来检测大于1的多个数字。
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
string[] inputs = input.Split();
int max = Convert.ToInt32(inputs[0]);
int amount = Convert.ToInt32(inputs[1]);
int path = max;
int how_many = 1; // said one is 1+1+1...
int x = 2;
while (x <= amount)
{
while (path >= x) //only 1 and another number
{
path -= x;
how_many += 1;
}
path = max;
x += 1;
}
Console.WriteLine(how_many);
Console.ReadKey();
}
抱歉,不是C#,而是python3解决方案(递归),仅在我不是Windows粉丝的情况下适用于C#:
def solution(n, m):
if n < 2 or m < 2:
return 1
if n < m:
return solution(n, n) ## or m = n
return solution(n-m, m) + solution(n, m-1)
print(solution(5, 3))
#5
print(solution(8,4))
#15
#4+4, 4+3+1, 4+2+2, 4+2+1+1, 4+1+1+1+1
#3+3+2 3+3+1+1 3+2+2+1 3+2+1+1+1 3+1+1+1+1+1
#2+2+2+2 2+2+2+1+1 2+2+1+1+1+1 2+1+1+1+1+1+1 1+1+1+1+1+1+1+1+1
如果对于大的N和M而言太慢,只需使用数组或列表进行存储较早的结果,它只是“技术”。