输入的 C++ 排列
c++ permutations of input
所以我试图在 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
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么不;名字在地图上是按顺序排列的吗
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 找到具有最多子串栅栏的字符串排列
- 重新排列单线以形成闭合多边形?
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 输入的 C++ 排列
- 公共/私有/受保护是否会更改内存中结构的排列?
- 如何在 C/C++ 中生成具有 n 组 5 个值(重复项)的所有可能排列的矩阵
- 在向量C++中排列奇数和偶数
- 如何进行排列?
- 矩阵行求和与 RowMajor 和 ColMajor 数据排列的奇怪性能差异
- 在 CPP 中打印具有重复项的选定长度字符的所有排列
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 调用参数排列不变函数 f(i++, i++)
- 以 C++ 为单位具有输出限制的排列
- 在C++中寻找排列和组合
- 在 std::map 中重新排列键
- 为什么我得到长度仅为 3 的字符串排列的输出?