提问者:小点点

C++输入置换


所以我试着让用户输入几个不同的数字,然后输出这些数字的各种可能的排列,用C++。 目前,它最多可用于三个不同的数字。 然而,我不知道是什么阻止了它接受更多的数字,我希望在这里得到一些答案。

此外,只要它起作用; 即使当输入仅为单个数字时,it分割也会出错。

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

#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;
}

共1个答案

匿名用户

我建议扔掉你的代码,重新开始。 你把它搞得太复杂了,所以更难解决它。

把你的主要功能分成两个部分:

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

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

std::vector<int> 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 

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|输入|置换)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?