2

假设给我:

  1. 整数范围iRange(即从1iRange)和
  2. 所需数量的组合

我想找到所有可能组合的数量并打印出所有这些组合。

例如:

给定iRange = 5n = 3

那么组合的数量就是iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10组合,输出为:

123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345

另一个例子:

给定iRange = 4n = 2

那么组合的数量就是iRange! / ((iRange!-n!)*n!) = 4! / (4-2)! * 2! = 6组合,输出为:

12 - 13 - 14 - 23 - 24 - 34

到目前为止,我的尝试是:

#include <iostream>
using namespace std;

int iRange= 0;
int iN=0;

int fact(int n)
{
    if ( n<1)
        return 1;
    else
    return fact(n-1)*n;
}

void print_combinations(int n, int iMxM)
{
    int iBigSetFact=fact(iMxM);
    int iDiffFact=fact(iMxM-n);
    int iSmallSetFact=fact(n);
    int iNoTotComb = (iBigSetFact/(iDiffFact*iSmallSetFact));
    cout<<"The number of possible combinations is: "<<iNoTotComb<<endl;
    cout<<" and these combinations are the following: "<<endl;


    int i, j, k;
    for (i = 0; i < iMxM - 1; i++)
    {
        for (j = i + 1; j < iMxM ; j++)
        {
            //for (k = j + 1; k < iMxM; k++)
                cout<<i+1<<j+1<<endl;
        }
    }
}

int main()
{
    cout<<"Please give the range (max) within which the combinations are to be found: "<<endl;
    cin>>iRange;
    cout<<"Please give the desired number of combinations: "<<endl; 
    cin>>iN;
    print_combinations(iN,iRange);
    return 0;   
}

我的问题: 我的代码中与组合打印相关的部分仅适用于n = 2, iRange = 4并且我不能使其一般工作,即适用于任何nand iRange

4

6 回答 6

2

看起来是一个很好的递归问题。

定义一个函数f(prefix, iMin, iMax, n),它打印n[ iMin, iMax] 范围内的所有数字组合并返回组合总数。对于n= 1,它应该打印从iMintoiMax和 return的每个数字iMax - iMin + 1

对于您的iRange = 5情况n = 3,您致电f("", 1, 5, 3). 输出应该是123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345.

请注意,第一组输出只是简单地1在 的输出上加上前缀f("", 2, 5, 2),即f("1", 2, 5, 2),后跟f("2", 3, 5, 2)f("3", 4, 5, 2)。看看你会如何用循环来做到这一点。在此之间,n上面 for = 1 的情况,以及错误输入的陷阱(最好是不打印任何内容并返回 0,它应该会简化您的循环),您应该能够编写f().

我停下来,因为这看起来像是一项家庭作业。这足以让你开始吗?

编辑:只是为了傻笑,我写了一个 Python 版本。Python 可以更轻松地处理事物的集合和列表并保持清晰。

#!/usr/bin/env python

def Combos(items, n):
    if n <= 0 or len(items) == 0:
        return []
    if n == 1:
        return [[x] for x in items]
    result = []
    for k in range(len(items) - n + 1):
        for s in Combos(items[k+1:], n - 1):
            result.append([items[k]] + s)
    return result

comb = Combos([str(x) for x in range(1, 6)], 3)
print len(comb), " - ".join(["".join(c) for c in comb])

请注意,Combos()它不关心items列表中项目的类型。

于 2009-12-09T20:53:29.630 回答
2

您的解决方案仅适用于 n=2。考虑使用具有 n 个整数的数组(梳子),然后循环将勾选数组中的最后一项。当该项目达到最大更新时,然后组合 [n-2] 项目并将最后一个项目设置为前一个值 +1。

基本上像时钟一样工作,但您需要逻辑来找到要上升的内容以及下一个最小值是什么。

于 2009-12-09T20:15:51.813 回答
1

这是您使用递归解决方案编辑的代码:D:D :

#include <iostream>

int iRange=0;   
int iN=0;           //Number of items taken from iRange, for which u want to print out the combinations
int iTotalCombs=0;
int* pTheRange;
int* pTempRange;

int find_factorial(int n)
{
    if ( n<1)
        return 1;
    else
    return find_factorial(n-1)*n;
}

//--->Here is another solution:
void print_out_combinations(int *P, int K, int n_i) 
{
    if (K == 0)
    {
        for (int j =iN;j>0;j--)
        std::cout<<P[j]<<" ";
        std::cout<<std::endl;
    }
    else
        for (int i = n_i; i < iRange; i++) 
        {
            P[K] = pTheRange[i];
            print_out_combinations(P, K-1, i+1);
        }
}
//Here ends the solution...

int main() 
{
    std::cout<<"Give the set of items -iRange- = ";
    std::cin>>iRange;
    std::cout<<"Give the items # -iN- of iRange for which the combinations will be created = ";
    std::cin>>iN;

    pTheRange = new int[iRange];
    for (int i = 0;i<iRange;i++)
    {
        pTheRange[i]=i+1;
    }
    pTempRange = new int[iN];

    iTotalCombs = (find_factorial(iRange)/(find_factorial(iRange-iN)*find_factorial(iN)));

    std::cout<<"The number of possible combinations is: "<<iTotalCombs<<std::endl;
    std::cout<<"i.e.the combinations of "<<iN<<" elements drawn from a set of size "<<iRange<<" are: "<<std::endl;
    print_out_combinations(pTempRange, iN, 0);
    return 0;
}
于 2009-12-12T02:18:23.830 回答
1

这是一个简单的递归解决方案的示例。如果你用循环替换递归,我相信会有一个更优化的实现。这可能是你的作业:)

#include <stdio.h>

const int iRange = 9;
const int n = 4;


// A more efficient way to calculate binomial coefficient, in my opinion
int Cnm(int n, int m)
{
    int i;
    int result = 1;

    for (i = m + 1; i <= n; ++i)
        result *= i;

    for (i = n - m; i > 1; --i)
        result /= i;

    return result;
}


print_digits(int *digits)
{
    int i;
    for (i = 0; i < n; ++i) {
        printf("%d", digits[i]);
    }
    printf("\n");
}

void plus_one(int *digits, int index)
{
    int i;

    // Increment current digit
    ++digits[index];

    // If it is the leftmost digit, run to the right, setup all the others
    if (index == 0) {
        for (i = 1; i < n; ++i)
            digits[i] = digits[i-1] + 1;
    }
    // step back by one digit recursively
    else if (digits[index] > iRange) {
        plus_one(digits, index - 1);
    }
    // otherwise run to the right, setting up other digits, and break the recursion once a digit exceeds iRange
    else {
        for (i = index + 1; i < n; ++i) {
            digits[i] = digits[i-1] + 1;

            if (digits[i] > iRange) {
                plus_one(digits, i - 1);
                break;
            }
        }
    }
}

int main()
{
    int i;
    int digits[n];

    for (i = 0; i < n; ++i) {
        digits[i] = i + 1;
    }

    printf("%d\n\n", Cnm(iRange, n));

    // *** This loop has been updated ***
    while (digits[0] <= iRange - n + 1) {
        print_digits(digits);
        plus_one(digits, n - 1);
    }

    return 0;
}
于 2009-12-09T21:54:23.743 回答
0

这是我的 C++ 函数,具有不同的接口(基于 sts::set)但执行相同的任务:

typedef std::set<int> NumbersSet;
typedef std::set<NumbersSet> CombinationsSet;

CombinationsSet MakeCombinations(const NumbersSet& numbers, int count)
{
  CombinationsSet result;

  if (!count) throw std::exception();

  if (count == numbers.size())
  {
    result.insert(NumbersSet(numbers.begin(), numbers.end()));
    return result;
  }

  // combinations with 1 element
  if (!(count - 1) || (numbers.size() <= 1))
  {
    for (auto number = numbers.begin(); number != numbers.end(); ++number)
    {
      NumbersSet single_combination;
      single_combination.insert(*number);
      result.insert(single_combination);
    }
    return result;
  }

  // Combinations with (count - 1) without current number
  int first_num = *numbers.begin();
  NumbersSet truncated_numbers = numbers;
  truncated_numbers.erase(first_num);
  CombinationsSet subcombinations = MakeCombinations(truncated_numbers, count - 1);

  for (auto subcombination = subcombinations.begin(); subcombination != subcombinations.end(); ++subcombination)
  {
    NumbersSet cmb = *subcombination;
    // Add current number
    cmb.insert(first_num);
    result.insert(cmb);
  }

  // Combinations with (count) without current number
  subcombinations = MakeCombinations(truncated_numbers, count);
  result.insert(subcombinations.begin(), subcombinations.end());

  return result;
}
于 2014-06-23T12:27:47.613 回答
0

我创建了一个next_combination()类似于 的函数next_permutation(),但需要有效输入才能使其工作

//nums should always be in ascending order

    vector <int> next_combination(vector<int>nums, int max){
    int size = nums.size();
    
    if(nums[size-1]+1<=max){
        nums[size-1]++;
        return nums;
    }else{
        if(nums[0] == max - (size -1)){
            nums[0] = -1;
            return nums; 
        }
        
        int pos;
        int negate = -1;
        for(int i = size-2; i>=0; i--){
            if(nums[i]+1 <= max + negate){
                pos = i;
                break;
            }
            negate --;
        }
        nums[pos]++;
        pos++;
        while(pos<size){
            nums[pos] = nums[pos-1]+1;
            pos++;
        }
    }
    return nums;
}
于 2021-01-24T01:05:58.563 回答