组合数(2)获取C(n,k)组合数列表的QT实现

1)工程文件

QT = core

CONFIG += c++17 cmdline

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2)主程序

#include 
#include 
#include 

using namespace std;
const int N = 10;
const int K = 5;

vector> getCombinationvector(int nums[],int ncount)
{
    vector> retvv = vector>();
    retvv.push_back(vector());
    for (int n=0; n newSub = vector(retvv[i]);
            newSub.push_back(n);
            retvv.push_back(newSub);
        }
    }
    return retvv;
}

void showCombinationvector( vector> va )
{
    std::cout << "C(" << N <<"," << K <<") = " << va.size() << "\n";
    std::cout << "[ \n";
    for (int var = 0; var < va.size(); ++var) {
        std::cout << "  [ ";
        for(int j=0; j< va[var].size();j++)
        {
            std::cout << va[var][j] << " ";
        }
        std::cout << "]\n";
    }

    std::cout << "]\n";
}

vector> getCombineNK(int n, int k) {
    vector nums = vector();
    for(int i = 1; i < k + 1; ++i)
        nums.push_back(i);
    nums.push_back(n + 1);

    vector> retvv = vector>();
    int j = 0;
    while (j < k) {
        retvv.push_back(vector(nums.begin(), nums.begin() + k));
        j = 0;
        while ((j < k) && (nums[j + 1] == nums[j] + 1))
        {
            nums[j] = j + 1;
            j++;
        }

        nums[j] ++ ;

    }
    return retvv;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int test[] = {1,2,3,4,5};
    //vector> va = getCombinationvector(test, 5);

    vector> va = getCombineNK(10, 5);

    showCombinationvector(va);
    return a.exec();
}

3)运行结果

C(10,5) = 252

  [ 1 2 3 4 5 ]
  [ 1 2 3 4 6 ]
  [ 1 2 3 5 6 ]
  [ 1 2 4 5 6 ]
  [ 1 3 4 5 6 ]
  [ 2 3 4 5 6 ]
  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