简体   繁体   English

递归置换(计数规则)

[英]Recursive Permutation (counting rule)

I looked through a bunch of questions and I didn't find this. 我看了一堆问题,但没有找到。 I need to write a recursive method that returns a Permutation, P(N,K). 我需要编写一个返回Permutation P(N,K)的递归方法。 Meaning, pool of 20 objects, draw 3, how many possibilities are there for the order you draw them in? 意思是,有20个对象的池,绘制3个,绘制它们的顺序有多少种可能性? The answer is 20*19*18. 答案是20 * 19 * 18。

Here's what I've got so far: 到目前为止,这是我得到的:

public double perm(long N, long K)

{
   if (K == 1)
      return N;
   else
      return perm((N*(N-1)), (K-1));
}

N is the pool, K is how many pulls. N是游泳池,K是多少拉。 My problem is figuring out how to make the "N*(N-1)*(N-2)..." bit work. 我的问题是弄清楚如何使“ N *(N-1)*(N-2)...”位起作用。 Say I do this: 说我这样做:

perm(10,3)

After my first time through the code I've got, N would be 10*9, or 90, which means that on the second loop, it'd be calculating 90*89, not (10*9)*8. 在我第一次获得代码后,N将是10 * 9或90,这意味着在第二个循环中,它将计算90 * 89,而不是(10 * 9)* 8。 I can't figure out how this is supposed to work, but the professor assigned it so it must be possible. 我不知道这应该如何工作,但是教授指定了它,因此它必须可行。

I could do this really easily with a FOR lop, but it can't be a for loop. 我可以使用FOR lop轻松地做到这一点,但不能成为for循环。 I don't really want the solution, just some guidance. 我真的不想要解决方案,只是一些指导。 Thanks! 谢谢!

You're recursing (is that a word?) with the multiplication result. 您将乘以结果递归(是一个单词吗?)。

return perm((N*(N-1)), (K-1));

Here, N*(N-1) = 90 (N being 10 initially), hence the result perm(90), k-1) 这里,N *(N-1)= 90(N最初为10),因此结果为perm(90), k-1)

You should multiply the result of P(N-1, K-1) with N like : 您应该将P(N-1,K-1)的结果与N相乘:

N * P(N-1, K-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM