c++ - c++ 输入的排列
问题描述
因此,我试图接受几个不同数字的用户输入,并在 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] != '\0') {
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
推荐阅读
- reactjs - 如何在 React JS 的表格单元格中嵌入动态下拉菜单?
- flutter - Flutter:如何通过 ToggleButtons 对 ListView 进行排序
- clion - 我的 clion 运行默认“CreateProcess 错误 = 216”
- java - 如何获取包含一些空项目的地图中的最后一个索引
- python - 我怎样才能正确忽略python中的错误?
- c++ - 用于获取 ac 程序中使用的所有头文件的 C++ 正则表达式
- ssl - Wget 和 Curl 停止使用 HTTPS。错误地抱怨证书过期
- r - 将列表的元素放入R中的访问器
- r - 如何手动计算库克距离
- python - Discord.py SSLCertVerificationError