组合数(1) 用Vector实现获取所有组合数列表的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;


vector> getCombinationList(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 showCombinationList( vector> va )
{
    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";
}

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

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

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

3.执行结果


  [ ]
  [ 0 ]
  [ 1 ]
  [ 0 1 ]
  [ 2 ]
  [ 0 2 ]
  [ 1 2 ]
  [ 0 1 2 ]
  [ 3 ]
  [ 0 3 ]
  [ 1 3 ]
  [ 0 1 3 ]
  [ 2 3 ]
  [ 0 2 3 ]
  [ 1 2 3 ]
  [ 0 1 2 3 ]
  [ 4 ]
  [ 0 4 ]
  [ 1 4 ]
  [ 0 1 4 ]
  [ 2 4 ]
  [ 0 2 4 ]
  [ 1 2 4 ]
  [ 0 1 2 4 ]
  [ 3 4 ]
  [ 0 3 4 ]
  [ 1 3 4 ]
  [ 0 1 3 4 ]
  [ 2 3 4 ]
  [ 0 2 3 4 ]
  [ 1 2 3 4 ]
  [ 0 1 2 3 4 ]
]

你可能感兴趣的:(qt,开发语言)