我基本上想要創建由三個操作符號組成的字符串(例如:+-*
或++/
或+++
)。這些字符串中的每一個應推入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));
被壓入載體(如字符串)的排列是:但
+-*/
+-/*
+/*-
+/-*
-*+/
-*/+
-+*/
-+/*
-/*+
-/+*
/*+-
/*-+
/+*-
/+-*
/-*+
/-+*
我想要什麼是有我的排列存在這樣的:
- 每個長度應該在三個字符
- 每個可能的組合,包括一個字符重複超過一次的字符必須存在。
我希望它被組織成這樣:
+++
---
***
///
/*/
+-+
++*
**/
etc...
我怎樣才能做到這一點?
你嘗試只用了一堆嵌套的'for'循環? – wlyles
這裏有一些很酷的技巧http://stackoverflow.com/questions/2211915/combination-and-permutation-in-c/ – doctorlove