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


例如:现在有一批鞋子,其中颜色有[‘白色’,‘黑色’,‘灰色’];大小有[‘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“程序员成长日志”,分享日常工作中解决的问题即可赚取稿费,大家一起成长~

猜你喜欢

转载自www.cnblogs.com/czrz1024/p/12652074.html