3

+-*基本的には、3 つの操作記号 (例:または++/または)で構成される文字列を作成したいと考えています+++。これらの文字列はそれぞれにプッシュする必要がありvector <string> opPermutations ます これまでの私のコードは次のとおりです:

 // Set up permutations for operators

string operatorBank[4] = {"+","-","*","/"};

 do {

    string currentPerm = operatorBank[0] + operatorBank[1] + operatorBank[2] + operatorBank[3];

    this -> opPermutations.push_back(currentPerm);

} while ( std::next_permutation(operatorBank, operatorBank + 4) );

ベクトルに (文字列として) プッシュされる順列は次のとおりです。

+-*/                                                                                                                                                                                           
+-/*                                                                                                                                                                                           
+/*-                                                                                                                                                                                           
+/-*                                                                                                                                                                                           
-*+/                                                                                                                                                                                           
-*/+                                                                                                                                                                                           
-+*/                                                                                                                                                                                           
-+/*                                                                                                                                                                                           
-/*+                                                                                                                                                                                           
-/+*                                                                                                                                                                                           
/*+-                                                                                                                                                                                           
/*-+                                                                                                                                                                                           
/+*-                                                                                                                                                                                           
/+-*                                                                                                                                                                                           
/-*+                                                                                                                                                                                           
/-+*  

しかし、私が望むのは、私の順列が次のように存在することです:

  • それぞれの長さは 3 文字である必要があります
  • 文字が複数回繰り返されるものを含め、すべての可能な順列が存在する必要があります。

次のように整理したいと思います。

+++
---
*** 
/// 
/*/
+-+
++*
**/

etc...

どうすればこれを達成できますか?

4

3 に答える 3

2

再帰を使用して、要求したものを出力します。順列文字列をベクトルに格納するように適応させることは簡単です。私は C++ プログラマーではないので、C++ で行うより良い方法があるかもしれません。しかし、ここでの主なアイデアは、再帰を使用することです。

    #include <iostream>
    #include <string>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;

    void displayPermutation(string permutation[], int length){
        int i;
        for (i=0;i<length;i++){
            cout<<permutation[i];
        }
        cout << endl;
    }

    void getPermutations(string operatorBank[], int operatorCount, 
            string permutation[],int permutationLength, int curIndex){
        int i;
        //stop recursion condition
        if(curIndex == permutationLength){
            displayPermutation(permutation,permutationLength);
        }
        else{
            for(i = 0; i < operatorCount; i++){
                permutation[curIndex] = operatorBank[i];
                getPermutations(operatorBank,operatorCount,permutation,
                    permutationLength,curIndex+1);
            }
        }
    }

    int main ()
   {
       int operatorCount = 4;
       int permutationLength = 3;
       string operatorBank[] = {"+","-","*","/"};
       string permutation[] = {"","","",""}; //empty string
       int curIndex = 0;
       getPermutations(operatorBank,operatorCount,permutation,
                                   permutationLength,curIndex);
       return 0;
   }

出力:

   +++
   ++-
   ++*
   ++/
   +-+
   +--
   +-*
   +-/
   +*+
   +*-
   +**
   +*/
   +/+
   +/-
   +/*
   +//
   .
   .
  and so on.
于 2013-07-29T22:58:52.247 に答える