如何编写'n'字符串的所有组合?重复:C = n!/(n-k)!

时间:2013-06-19 20:17:12

标签: c++ string combinations

很抱歉,如果问题不明确,我想知道如何使用公式C=n!/(n-k)!编写可以输出一些句子的所有组合的C ++程序。例如,这就是我试图打印的那种东西:

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...

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

EDIT。

我尝试了以下程序,但我无法找到改变上述公式中的内容的方法" 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;
}

2 个答案:

答案 0 :(得分:2)

你可能意味着你想要'n'字符串的所有可能组合。 有n!可能的情况。 你可以使用std::next_permutation 方法如下:

我想你的所有句子都是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;
}

这是一个简单的打印示例。 如果你有超过4个字符串,在do-while循环中你会使用类似this

的东西

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()) );
  

此外,是否有可能有多达10亿的组合或那里   有一些限制吗?

唯一的限制是记忆。 在此示例中,您只有1个存储所有字符串的向量。 所以,如果你有10个字符串,你将有10个! = 3,628,800种不同的组合,但内存本身只是你的矢量有10个字符串使用的内存。

答案 1 :(得分:1)

您可以使用next_permutation执行此操作。