如果假设数字X = A1 * A2 * ... * An,其中A1,A2,..,An是X的因子。 我们可以通过多少种方式安排这些因素,这些因素一次2个为父母的身份?
假设X有5个因子,如g,h,j,k 表达式ghjk可以用5种方式评估,即(((gh)j)k),(g(h(jk))),(g((hj)k)),((g(hj))k)和( (GH)(JK))
所以输入为 ghjk
输出应该是 5
如何在C / C ++中执行此操作?
给出1< N< 34 ..
答案 0 :(得分:0)
我认为你的意思是“X有5个因子,如g,h,j,k,i”你只为上面的X放了4个因子。 看起来你正在寻找5组中2个元素的组合。
这不是S的简单组合选择K而不重复:
N! / k!(n-k)!
其中n是集合中元素的数量,k是k组合。
对你而言,它将是5个集合中的2个子集:(n = 5,k = 2)
5! / 2!*(5-2)! = 5! /(2!* 3!)= 10
(g,h),(g,j),(g,k),(g,i)
(h,j),(h,k),(h,i)
(j,k),(j,i)
(K,I)
因此,在一组5中,有2种因素实际上有10种可能的组合。
答案 1 :(得分:0)
不改变因子的顺序,n
因子的乘积可以C(n-1)
方式括起来,其中C(k)
是k
- {{3} },
C(k) = 1/(k+1) * \binom(2*k, k)
= 1/(k+1) * (2k)!/(k!)²
由于n
因子乘积的(完全)括号等同于构建带有n
叶子的二叉树,这也是具有{{1}的二叉树的不同结构的数量离开。
如果允许更改因子的顺序,则必须将其乘以因子的不同顺序的数量,如果没有重复因子,则只需n
,并且
n!
如果n!/(a_1! * ... * a_k!)
个不同因素分别具有多重性k
。
答案 2 :(得分:0)
您的问题是使用Catalan numbers:Cn是为关联表达式括起句法的方式。它也是具有n + 1个叶子的可能全二进制树的数量(完整二叉树=一个树,其中每个节点恰好有2个子节点,或者是一个离开)。
因此,可以use recursion to generate all combinations:
#include <iostream>
#include <string>
#include <vector>
typedef std::vector<std::string> str_vector;
// Combine u and v, and add the result to w
// u and v contains all possible combinations with respectively m and n - m letters
// So w will contain combinations with n letters
void combine(str_vector& w, const str_vector& u, const str_vector& v)
{
for(unsigned int t = 0; t < u.size(); t++)
for(unsigned int i = 0; i < v.size(); i++)
w.push_back("(" + u[t] + v[i] + ")");
}
// The function resolving our problem
// It output an array containing all possible combinations
// with n letters, starting with startLetter
str_vector problem(unsigned int n, char startLetter)
{
if(n == 1)
// Only one combination with one letter.
// Array with 1 string which contains only one character
return str_vector(1,std::string(1,startLetter));
else
{
str_vector w;
// Get all combinations for n-1 letters, and add them to w
for(unsigned int t = 1; t < n; t++)
combine(w, problem(t, startLetter), problem(n - t, startLetter + (char)t));
return w;
}
}
int main()
{
// Get all possible combinations with 4 letters
const str_vector& r = problem(4,'a');
// Print all solutions
for(unsigned int t = 0; t < r.size(); t++)
std::cout << r[t] << "\n";
}
此代码不是最快的也不是最优化的。但是,我认为它非常易读。
对于组合的数量,公式为:
这可以简化为:
(2n)!
Cn = ---------
n!² (n+1)
计算Cn为您提供具有n + 1个标识符的组合数。但是,它并没有为您提供解决方案。