以CPP重复打印所选长度字符的所有排列

问题描述 投票:0回答:1

此代码仅打印数字!我应该怎么打印字符串?!

例如,我用ABC选择5作为长度,输出应该是:

aaaaa
aaaab
aaaac
aaaba
aaabb
aaabc
aaaca
aaacb
aaacc
aabaa
aabab
aabac
aabba
aabbb
aabbc
aabca
aabcb
aabcc
aacaa
aacab
aacac
aacba
aacbb
aacbc
aacca
aaccb
aaccc
abaaa
abaab
abaac
ababa
ababb
ababc
abaca
abacb
abacc
abbaa
abbab
abbac
abbba
abbbb
abbbc
abbca
abbcb
abbcc
abcaa
abcab
abcac
abcba
abcbb
abcbc
abcca
abccb
abccc
acaaa
acaab
acaac
acaba
acabb
acabc
acaca
acacb
acacc
acbaa
acbab
acbac
acbba
acbbb
acbbc
acbca
acbcb
acbcc
accaa
accab
accac
accba
accbb
accbc
accca
acccb
acccc
baaaa
baaab
baaac
baaba
baabb
baabc
baaca
baacb
baacc
babaa
babab
babac
babba
babbb
babbc
babca
babcb
babcc
bacaa
bacab
bacac
bacba
bacbb
bacbc
bacca
baccb
baccc
bbaaa
bbaab
bbaac
bbaba
bbabb
bbabc
bbaca
bbacb
bbacc
bbbaa
bbbab
bbbac
bbbba
bbbbb
bbbbc
bbbca
bbbcb
bbbcc
bbcaa
bbcab
bbcac
bbcba
bbcbb
bbcbc
bbcca
bbccb
bbccc
bcaaa
bcaab
bcaac
bcaba
bcabb
bcabc
bcaca
bcacb
bcacc
bcbaa
bcbab
bcbac
bcbba
bcbbb
bcbbc
bcbca
bcbcb
bcbcc
bccaa
bccab
bccac
bccba
bccbb
bccbc
bccca
bcccb
bcccc
caaaa
caaab
caaac
caaba
caabb
caabc
caaca
caacb
caacc
cabaa
cabab
cabac
cabba
cabbb
cabbc
cabca
cabcb
cabcc
cacaa
cacab
cacac
cacba
cacbb
cacbc
cacca
caccb
caccc
cbaaa
cbaab
cbaac
cbaba
cbabb
cbabc
cbaca
cbacb
cbacc
cbbaa
cbbab
cbbac
cbbba
cbbbb
cbbbc
cbbca
cbbcb
cbbcc
cbcaa
cbcab
cbcac
cbcba
cbcbb
cbcbc
cbcca
cbccb
cbccc
ccaaa
ccaab
ccaac
ccaba
ccabb
ccabc
ccaca
ccacb
ccacc
ccbaa
ccbab
ccbac
ccbba
ccbbb
ccbbc
ccbca
ccbcb
ccbcc
cccaa
cccab
cccac
cccba
cccbb
cccbc
cccca
ccccb
ccccc

代码:

#include <stdio.h>
#include <list>
#include <string>
#include <iostream>
using namespace std;



int outputDigit;
string input[1000];
int num;
string final;
int z=0;


struct Generator
{





public:




    Generator(int s)
            : cSlots(s)

    {
        a = new int[s];

        for (int i = 0; i < cSlots - 1; i++) {
            a[i] = 1;
        }
        a[cSlots - 1] = 0;

        nextInd = cSlots;
    }

    ~Generator()
    {
        delete a;
    }

    bool doNext()
    {
        for (;;)
        {
            if (a[nextInd - 1] == cValues) {
                nextInd--;
                if (nextInd == 0)
                    return false;
            }
            else {
                a[nextInd - 1]++;
                while (nextInd < cSlots) {
                    nextInd++;
                    a[nextInd - 1] = 1;
                }

                return true;
            }
        }
    }

    void doPrint()
    {

        for (int i = 0; i < cSlots; i++) {
            num=a[i];
            cout<<num;


        }


        printf("\n");
    }

public:
    int *a;
    int cSlots;
    int cValues;
    int nextInd;
};






int main()
{



    cout<<"enter outputDigits";
    cin>>outputDigit;



    Generator g(outputDigit);

    while (g.doNext()) {
        g.doPrint();
    }

    return 0;
}

但输出是

enter outputDigits5
11121
11122
11123
11124
11125
11126
11127
11128
11129
111210
111211
111212
111213
111214
111215
111216
111217
111218
111219
111220
111221
111222
111223
111224
111225
111226
111227
111228
111229
111230
111231
111232
111233
111234
111235......
c++ permutation
1个回答
0
投票

cValues是什么?

我知道此答案不会直接给您正确的答案。但是,我建议您确保您:

•初始化构造函数中的所有属性。

•避免使用全局成员。

•使用更具描述性的名称。

•尝试使用较小的输入(例如2或3)首先重现您的错误。

问候,

© www.soinside.com 2019 - 2024. All rights reserved.