如何编写';n';串?重复:C=n/(n-k)! 对不起,如果问题不清楚,我想知道如何用C++编写一个程序,可以输出所有句子的组合,使用公式 c= n!(n-k)。例如,这就是我想要打印的东西: combination no 1: sentence1 sentence2 sentence3 sentence4 combination no 2: sentence1 sentence2 sentence4 sentence3 combination no 3: sentence1 sentence3 sentence2 sentence4 combination no 4: sentence1 sentence3 sentence4 sentence2 combination no 5: sentence1 sentence4 sentence3 sentence2 combination no 6: sentence1 sentence4 sentence2 sentence3 And so on...

如何编写';n';串?重复:C=n/(n-k)! 对不起,如果问题不清楚,我想知道如何用C++编写一个程序,可以输出所有句子的组合,使用公式 c= n!(n-k)。例如,这就是我想要打印的东西: combination no 1: sentence1 sentence2 sentence3 sentence4 combination no 2: sentence1 sentence2 sentence4 sentence3 combination no 3: sentence1 sentence3 sentence2 sentence4 combination no 4: sentence1 sentence3 sentence4 sentence2 combination no 5: sentence1 sentence4 sentence3 sentence2 combination no 6: sentence1 sentence4 sentence2 sentence3 And so on...,c++,string,combinations,C++,String,Combinations,此外,是否可能有多达10亿个组合或存在一些限制 编辑 我尝试了下面的程序,但是我找不到一种方法来改变上面公式中的“k”变量 // next_permutation example #include <iostream> // std::cout #include <algorithm> // std::next_permutation, std::sort #include <string> // std::string #inclu

此外,是否可能有多达10亿个组合或存在一些限制

编辑

我尝试了下面的程序,但是我找不到一种方法来改变上面公式中的“k”变量

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort
#include <string>       // std::string
#include <vector>       // std::vector

int main () {
  std::string sentence1 = " A Sentence number one ";
  std::string sentence2 = " B Sentence number two ";
  std::string sentence3 = " C Sentence number three ";
  std::string sentence4 = " D Sentence number four ";

  // Store all the elements in a container ( here a std::vector)
  std::vector<std::string> myVectorOfStrings;      
  // In the vector we add all the sentences.
  // Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
  myVectorOfStrings.push_back(sentence1);
  myVectorOfStrings.push_back(sentence2);
  myVectorOfStrings.push_back(sentence3);
  myVectorOfStrings.push_back(sentence4);

  // The elements must be sorted to output all the combinations
  std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());


  std::cout << "The 4! possible permutations with 4 elements:\n";
  do {
    //This printing can be improved to handle any number of sentences, not only four.
    std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
  } while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );

  std::cout << "After loop: "  << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';

  return 0;
}
//下一个置换示例
#include//std::cout
#包括//std::next\u置换,std::sort
#include//std::string
#include//std::vector
int main(){
std::string sentence1=“第一句话”;
std::string sentence2=“B第二句”;
std::string sentence3=“C第三句”;
std::string sentence4=“D第四句”;
//将所有元素存储在一个容器中(这里是std::vector)
std::向量myvectorofstring;
//在向量中,我们添加所有的句子。
//注意:我可以做字符串的矢量化;
myVectorOfStrings.向后推(第1句);
myVectorOfStrings.向后推(第2句);
myVectorOfStrings.向后推(第3句);
myVectorOfStrings.向后推(第4句);
//必须对元素进行排序以输出所有组合
排序(myVectorOfStrings.begin(),myVectorOfStrings.end());

std::cout您可以使用来执行此操作。

您的意思可能是您需要所有可能的“n”字符串组合。 有n个可能的案例。 你可以使用 以下是如何:

我想你所有的句子都是std::string,如下所示:

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort
#include <string>       // std::string
#include <vector>       // std::vector

int main () {
  std::string sentence1 = " A Sentence number one ";
  std::string sentence2 = " B Sentence number two ";
  std::string sentence3 = " C Sentence number three ";
  std::string sentence4 = " D Sentence number four ";

  // Store all the elements in a container ( here a std::vector)
  std::vector<std::string> myVectorOfStrings;      
  // In the vector we add all the sentences.
  // Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
  myVectorOfStrings.push_back(sentence1);
  myVectorOfStrings.push_back(sentence2);
  myVectorOfStrings.push_back(sentence3);
  myVectorOfStrings.push_back(sentence4);

  // The elements must be sorted to output all the combinations
  std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());


  std::cout << "The 4! possible permutations with 4 elements:\n";
  do {
    //This printing can be improved to handle any number of sentences, not only four.
    std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';
  } while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );

  std::cout << "After loop: "  << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << '\n';

  return 0;
}
//下一个置换示例
#include//std::cout
#包括

然后,do while循环将是:

do {
  //Print all the sentences in my vector :
  for( auto i = myVectorOfStrings.begin(); i != myVectorOfStrings.end(); ++i)
    std::cout << *i << ' ';
  // Go to the next line
  std::cout << std::endl;
} while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
do{
//打印我的向量中的所有句子:
for(自动i=myVectorOfStrings.begin();i!=myVectorOfStrings.end();+i)

std::cout
std::next_permutation
只是一个提示:“请为我编写我的整个程序”的请求并不是很受欢迎。好吧,我记下了。我如何更改公式中k的值?