基于位置计算组合 [英] Calculate Combination based on position
问题描述
创建给定的字符集合C的函数,可以生成第N个组合或返回该系列定开始位置(NS)的组合和结束位置(氖)和组合(MX)的最大长度。
Create a function that given a character set C, can generate the Nth combination OR return the series of combination given a starting position (Ns) and ending position (Ne) and the maximum length of the combination (Mx).
一个具体的例子:
让C = [A,B,C]
Let C = [A,B,C]
我们知道,不同的组合看起来像下面的假设的Mx = 3 (组合将是不同长度不同)的:
We know that different combinations would look like the following assuming Mx = 3 (the combination would be different for different lengths):
1. AAA
2. AAB
3. AAC
4. ABA
5. ABB
6. ABC
N. ... Etc
如果我们是通过下列参数:
If we was to pass the following parameters :
C = [A,B,C]的Mx = 3-NS = 3 NE = 3
C = [A,B,C] Mx = 3 Ns = 3 Ne = 3
我们希望以下结果:
AAC
如果我们是通过下列参数:
If we was to pass the following parameters :
C = [A,B,C]的Mx = 3-NS = 4 NE = 6
C = [A,B,C] Mx = 3 Ns = 4 Ne = 6
我们希望以下结果:
4. ABA
5. ABB
6. ABC
有关的方案中,编程语言是不相关的。但是C#是preferred。另外最重要的是如何解决它的解释。
For the solution, the programming language is not relevant. However C# would be preferred. Also most important would be an explanation of how its solved.
我期待着惊人的宗师的堆栈溢出...
I look forward to the amazing Guru's of Stack Overflow...
推荐答案
由于指数的 N (从0开始)到的 N 符号组合的顺序,你可以在我个符号,通过计算的 N / N 我 %<强> N (使用整数除法和余)
Given an index N (0-based) into the sequence of combinations of n symbols, you can get the i'th symbol by calculating N / ni % n (using integer division and remainder)
例如:
C = {A, B, C} (giving n = 3)
N = 6
i = 0 => 6 / 3^0 % 3 = 0 (symbol 0 = A)
i = 1 => 6 / 3^1 % 3 = 2 (symbol 2 = C)
i = 2 => 6 / 3^2 % 3 = 0 (symbol 0 = A)
Resulting sequence: ACA
的序列被视为一个碱基的 N 强>号,以及各个单独数字计算
The sequence is treated as a base-n number, and the individual digits are calculated.
这篇关于基于位置计算组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!