输入的 C++ 排列

c++ permutations of input

本文关键字:排列 C++ 输入      更新时间:2023-10-16

所以我试图在 c++ 中获取几个不同数字的用户输入,并输出这些数字的每个可能排列。目前,它最多适用于三个不同的号码。然而,我不知道是什么阻止了它接受更多的数字,并希望在这里得到一些答案。

此外,每当它起作用时;即使输入只有一个数字,它也会分段错误。

这是代码,有什么想法吗?

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
////////////////creating the list which'll be permutated
string korp = "64832965k";
cout << "input your numbers, signal that you're done with 'n' n";
vector<int> dude;
int fit = 0;
while (korp[0] != 'n') {
cin >> korp;
if (korp[0] != 'n') {
int thisis = 0;
int hit = 0;
while (korp[hit] != '') {
thisis = thisis * 10 + (korp[hit] - 48);
hit++;
}
dude.push_back(thisis);
fit++;
}
}
vector<bool> in(fit, false);
vector<int> intin(fit, 0);
vector<vector<bool> > minds;
minds.push_back(in);
vector<vector<int> > mines;
mines.push_back(intin);
/////////permutation of the list
int things = 0;
int seed = 0;
//permutation tree loop start
for (int curr = 0; curr < dude.size(); curr++) {
int tanks = things;
while (things == tanks) { //runs until the next element of the original list should be added
int position = 0;
for (int i = 0; i < fit; i++) {
if (dude[curr] == mines[seed][i] && minds[seed][i] == true) {
position = i;
}
}
while (position < fit) {
if (minds[seed][position] == false) {
minds.push_back(minds[seed]);
minds.back()[position] = true;
mines.push_back(mines[seed]);
mines.back()[position] = dude[curr];
}
position++;
}
seed++;
things = 0;
for (int q = 0; q < fit; q++) {
if (minds[seed][q] == true) {
things++;
}
}
}
}
//permutation tree loop end
////////outputs the permutations
minds.push_back(in);
mines.push_back(intin);
int whom = 0;
int bom = sizeof(minds);
while (whom < bom) {
bool oped = true;
for (int z = 0; z < fit; z++) { //checks if the current vector is a permutation
if (minds[whom][z] == false) {
oped = false;
}
}
if (oped == true) {
cout << '(';
for (int a = 0; a < fit; a++) {
cout << mines[whom][a] << ',';
}
cout << ')' << 'n';
}
whom++;
}
return 0;
}

我建议扔掉你的代码并重新开始。你把它弄得太复杂了,修复起来更难。

将主要功能分为两部分:

  • 读取输入
  • 打印排列
int main() {
const auto input = readInput();
printPermutations(input);
}

你把你的问题分成两个小问题,你已经解决了第一个问题。

auto readInput() {
std::string korp;
std::cout << "input your numbers, signal that you're done with 'n' n";
std::vector<int> dude;
do {
std::cin >> korp;
if (korp[0] != 'n') {
int thisis = 0;
for (const auto c : korp) {
thisis = thisis * 10 + (c - 48);
}
dude.push_back(thisis);
}
} while (korp[0] != 'n');
return dude;
}

对于第二个问题,您应该使用 STL。不要重新发明轮子。

首先用std::sort对容器进行排序。然后循环遍历您使用std::next_permutation获得的排列。

void printPermutations(std::vector<int> input) {
std::sort(std::begin(input), std::end(input));
do {
for (const auto num : input) {
std::cout << num << ' ';
}
std::cout << 'n';
} while (std::next_permutation(std::begin(input), std::end(input)));
}

必要的标头是

#include <algorithm>
#include <string>
#include <iostream>
#include <vector>

输入:

1 12 4 123 n

输出:

1 4 12 123 
1 4 123 12 
1 12 4 123 
1 12 123 4 
1 123 4 12 
1 123 12 4 
4 1 12 123 
4 1 123 12 
4 12 1 123 
4 12 123 1 
4 123 1 12 
4 123 12 1 
12 1 4 123 
12 1 123 4 
12 4 1 123 
12 4 123 1 
12 123 1 4 
12 123 4 1 
123 1 4 12 
123 1 12 4 
123 4 1 12 
123 4 12 1 
123 12 1 4 
123 12 4 1