码迷,mamicode.com
首页 > 编程语言 > 详细

qt-n个数组实现排列组合

时间:2020-04-07 11:11:21      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:get   成长   length   +=   point   lag   运动   bool   highlight   


例如:现在有一批鞋子,其中颜色有[‘白色’,‘黑色’,‘灰色’];大小有[‘40’,‘41’,‘42’],样式有[‘板鞋’,‘运动’,‘休闲’],要求写一个算法,实现[[‘白色’,‘40’,‘板鞋’], [‘白色’,‘42’,‘休闲’] …]这样的组合

代码如下

QList<QVector<QPointF>> xx::getAllGroup(QList<QVector<QPointF>> val){
  int oriSize = val.size();

  QVector<int> tempIndexArr(oriSize);
  tempIndexArr[oriSize - 1] = -1;
  QVector<int> tempLengthArr(oriSize);
  for (int i = 0; i < oriSize; i++){
    tempLengthArr[i] = val[i].size();
  }

  QList<QVector<QPointF>> newList;
  bool completeFlag = false;
  while (!completeFlag){
    int changeIndex = val.size() - 1;
    bool isRightIndex = false;
    while (!isRightIndex){
      tempIndexArr[changeIndex] += 1;
      if (tempIndexArr[changeIndex] >= tempLengthArr[changeIndex]){
        if (changeIndex == 0){
          isRightIndex = true;
          completeFlag = true;
        }
        else{
          tempIndexArr[changeIndex--] = 0;
        }
      }
      else{
        isRightIndex = true;
      }
    }
    if (isRightIndex && !completeFlag){
      QVector<QPointF> pointVec;
      for (int i = 0; i != val.size(); i++){
        pointVec.push_back(val[i][tempIndexArr[i]]);
      }
      newList.push_back(pointVec);
    }
  }
  return newList;
}

  已经工作的程序员朋友可以关注下我的gzh“程序员成长日志”,分享日常工作中解决的问题即可赚取稿费,大家一起成长~

qt-n个数组实现排列组合

标签:get   成长   length   +=   point   lag   运动   bool   highlight   

原文地址:https://www.cnblogs.com/czrz1024/p/12652074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!