QT和C++排列组合

界面比较简洁,如有需要请大家自行完善!!!

头文件

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_text.h"

class text : public QMainWindow
{
    Q_OBJECT

public:
    text(QWidget *parent = nullptr);
    ~text();

    void ParseStringToVector(const QString& strData, std::vector<std::vector<int>>& vecData);

    void Paxi(std::vector<std::vector<int>> all_num, std::vector<int> single_num, int num);
    std::vector<std::string> result_str;

private:
    Ui::textClass* m_ui;;
};

 

#include "text.h"
#include  <string.h>  
#include <iostream>

using namespace std;

text::text(QWidget* parent)
    : QMainWindow(parent),
    m_ui(new Ui::textClass)
{
    m_ui->setupUi(this);
    connect(m_ui->pushButton , &QPushButton::clicked, this, [=]
        {
            m_ui->listWidget->clear();
            QString m_input = m_ui->lineEdit->text();
            vector<vector<int>> m_listData;
            ParseStringToVector(m_input, m_listData);
            std::vector<int> single_num;
            Paxi(m_listData, single_num, 0);
        });

}
text::~text()
{
}

void text::ParseStringToVector(const QString& strData, vector<vector<int>>& vecData)
{
   
    vecData.clear();
    auto rows = strData.split(";");
    for (auto row : rows)
    {
        if (row == "")break;
        auto values = row.split(",");
        std::vector<int> rowData;
        for (auto value : values)
        {
            rowData.push_back(value.toInt());
        }
        vecData.push_back(rowData);
    }
}


void text::Paxi(std::vector<std::vector<int>> all_num, std::vector<int> single_num, int num)
{
    for (int i = 0; i < all_num[num].size(); i++)
    {
        if (num < all_num.size() - 1)
        {
            if (num < single_num.size())
            {
                single_num[num] = all_num[num][i];
            }
            else
            {
                single_num.push_back(all_num[num][i]);
            }
            Paxi(all_num, single_num, num + 1);
        }
        else
        {
            //single_num.push_back(all_num[num][i]);
            std::string str;
            for (int j = 0; j < single_num.size(); j++)
            {
                str += to_string(single_num[j]) + ",";
            }
            str += to_string(all_num[num][i]);
            result_str.push_back(str);
            m_ui->listWidget->addItem(QString::fromStdString(str));
        }
    }
}
 

主函数

#include "text.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    text w;
    w.show();
    return a.exec();
}
 

 

  • 39
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二维数组的排列组合可以使用嵌套循环实现。假设有一个二维数组arr[m][n],要将它中的元素进行排列组合,可以按照以下思路实现: 1. 外层循环控制行数,内层循环控制列数。 2. 内层循环中,将当前元素与后面的元素进行交换,然后递归调用排列组合函数。 3. 递归终止条件是当前行数等于总行数,表示已经排列完毕,将结果输出。 以下是一个简单的示例代码: ```c #include <stdio.h> void perm(int arr[][3], int m, int n, int row) { if (row == m) { // 输出结果 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", arr[i][j]); } printf("\n"); } printf("\n"); return; } for (int i = row; i < m; i++) { for (int j = 0; j < n; j++) { // 交换当前元素和后面的元素 int temp = arr[row][j]; arr[row][j] = arr[i][j]; arr[i][j] = temp; // 递归调用排列组合函数 perm(arr, m, n, row+1); // 恢复原始状态 temp = arr[row][j]; arr[row][j] = arr[i][j]; arr[i][j] = temp; } } } int main() { int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; perm(arr, 3, 3, 0); return 0; } ``` 在这个示例代码中,我们将一个3x3的二维数组arr进行排列组合,并输出结果。运行结果如下: ``` 1 2 3 4 5 6 7 8 9 1 2 3 4 5 9 7 8 6 1 2 3 4 6 5 7 8 9 1 2 3 4 6 9 7 8 5 1 2 3 4 5 6 7 9 8 1 2 3 4 5 9 7 6 8 1 2 3 4 6 5 7 9 8 1 2 3 4 6 9 7 5 8 1 2 3 4 5 6 9 8 7 1 2 3 4 5 9 9 8 6 1 2 3 4 6 5 9 8 7 1 2 3 4 6 9 9 8 5 1 2 3 5 4 6 7 8 9 1 2 3 5 4 9 7 8 6 1 2 3 5 6 4 7 8 9 1 2 3 5 6 9 7 8 4 1 2 3 5 4 6 7 9 8 1 2 3 5 4 9 7 6 8 1 2 3 5 6 4 7 9 8 1 2 3 5 6 9 7 4 8 1 2 3 5 4 6 9 8 7 1 2 3 5 4 9 9 8 6 1 2 3 5 6 4 9 8 7 1 2 3 5 6 9 9 8 4 1 2 3 6 4 5 7 8 9 1 2 3 6 4 9 7 8 5 1 2 3 6 5 4 7 8 9 1 2 3 6 5 9 7 8 4 1 2 3 6 4 5 7 9 8 1 2 3 6 4 9 7 5 8 1 2 3 6 5 4 7 9 8 1 2 3 6 5 9 7 4 8 1 2 3 6 4 5 9 8 7 1 2 3 6 4 9 9 8 5 1 2 3 6 5 4 9 8 7 1 2 3 6 5 9 9 8 4 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值