Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何选择所有可能的组合?_Algorithm_Combinations - Fatal编程技术网

Algorithm 如何选择所有可能的组合?

Algorithm 如何选择所有可能的组合?,algorithm,combinations,Algorithm,Combinations,假设我们有用户拥有的贷款列表,如下所示: 贷款1 贷款2 贷款3 ... 贷款10 我们的功能可以接受2到10笔贷款: 贷款。 例如,以下是可能的: 函数loan1,loan2 函数loan1,loan3 函数loan1,loan4 函数loan1、loan2、loan3 函数loan1、loan2、loan4 功能贷款1、贷款2、贷款3、贷款4、贷款5、贷款6、贷款7、贷款8、贷款9、贷款10 如何编写代码以将所有可能的组合传递给该函数?如果您已经用多种语言实现了生成组合,请自行选择。以下是我

假设我们有用户拥有的贷款列表,如下所示:

贷款1 贷款2 贷款3 ... 贷款10 我们的功能可以接受2到10笔贷款: 贷款。 例如,以下是可能的:

函数loan1,loan2 函数loan1,loan3 函数loan1,loan4 函数loan1、loan2、loan3 函数loan1、loan2、loan4 功能贷款1、贷款2、贷款3、贷款4、贷款5、贷款6、贷款7、贷款8、贷款9、贷款10
如何编写代码以将所有可能的组合传递给该函数?

如果您已经用多种语言实现了生成组合,请自行选择。

以下是我们在ruby中的实现方法:

loans= ['loan1','loan2', ... , 'loan10']

def my_function(loans)
  array_of_loan_combinations = (0..arr.length).to_a.combination(2).map{|i,j| arr[i...j]}

  array_of_loan_combinations.each do |combination|
    //do something
  end
end
致电:

my_function(loans);

我已经编写了一个类来处理处理处理二项式系数的常用函数,这是您的问题所属的问题类型。它执行以下任务:

将任意N选择K的所有K索引以良好格式输出到文件。可以用更具描述性的字符串或字母替换K索引。这种方法使得解决这类问题变得非常简单

将K索引转换为已排序二项系数表中某个条目的正确索引。这种技术比以前发布的依赖于迭代的技术要快得多。它通过使用帕斯卡三角形固有的数学特性来实现这一点。我的论文谈到了这一点。我相信我是第一个发现并发表这项技术的人,但我可能错了

将已排序的二项式系数表中的索引转换为相应的K索引。我相信它可能比你找到的链接要快

使用Mark Dominus方法计算二项式系数,该系数不太可能溢出,并且适用于较大的数字

该类是用.NETC编写的,它提供了一种通过使用泛型列表来管理与问题相关的对象(如果有的话)的方法。这个类的构造函数接受一个名为InitTable的bool值,如果为true,它将创建一个通用列表来保存要管理的对象。如果此值为false,则不会创建表。执行上述4种方法不需要创建表。提供了访问器方法来访问表

有一个关联的测试类,它显示了如何使用该类及其方法。它已经过2个案例的广泛测试,没有已知的bug

要阅读有关此类的信息并下载代码,请参阅将二项系数表化

将这个类转换为您选择的语言应该不难

为了解决您的问题,您可能需要编写一个新的loans函数,该函数将loan对象数组作为输入,并使用BinCoeff类处理这些对象。在C中,为了获得每个唯一组合的贷款数组,可以使用以下示例代码:

void LoanCombinations(Loan[] Loans)
{
   // The Loans array contains all of the loan objects that need
   // to be handled.
   int LoansCount = Loans.Length;
   // Loop though all possible combinations of loan objects.
   // Start with 2 loan objects, then 3, 4, and so forth.
   for (int N = 2; N <= N; N++)
   {
      // Loop thru all the possible groups of combinations.
      for (int K = N - 1; K < N; K++)
      {
         // Create the bin coeff object required to get all
         // the combos for this N choose K combination.
         BinCoeff<int> BC = new BinCoeff<int>(N, K, false);
         int NumCombos = BinCoeff<int>.GetBinCoeff(N, K);
         int[] KIndexes = new int[K];
         // Loop thru all the combinations for this N choose K.
         for (int Combo = 0; Combo < NumCombos; Combo++)
         {
            // Get the k-indexes for this combination, which in this case
            // are the indexes to each loan in Loans.
            BC.GetKIndexes(Loop, KIndexes);
            // Create a new array of Loan objects that correspond to
            // this combination group.
            Loan[] ComboLoans = new Loan[K];
            for (int Loop = 0; Loop < K; Loop++)
               ComboLoans[Loop] = Loans[KIndexes[Loop]];
            // Call the ProcessLoans function with the loans to be processed.
            ProcessLoans(ComboLoans);
         }
      }
   }
}

我没有测试上述代码,但一般来说,它应该可以解决您的问题。

什么语言?您可以使用函数LOAN,*列出几乎任何语言的贷款。@skjaidev我认为该函数只处理特定金融应用程序的给定贷款,因此只有给定的输入贷款用于分析,例如信用评分。我想他不是指每笔贷款time@skjaidev哦,我明白你现在的意思了,你的意思是他不知道如何接受变量输入,而我把他的问题理解为如何为一个接受变量输入的现有函数生成所有的组合。LA_u2;一些澄清可能是@skjaidev的副本,我正在寻找算法,因为该语言是特定于业务应用程序的。现在ruby已经为数组提供了名为combination和map的现成方法。如果您使用的语言没有这些函数,您可以创建这些函数。请查看组合和映射的作用,以便您可以使用回等价物或拥有自己的实现