What's a good way for figuring out all possible words of a given length

后端 未结 12 1759
一生所求
一生所求 2021-02-04 17:27

I\'m trying to create an algorithm in C# which produces the following output strings:

AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ

What is the

12条回答
  •  隐瞒了意图╮
    2021-02-04 17:53

    A very simple but awesome code that generates all words of 3 and 4 letters of english language

    #include 
    using namespace std;
    char alpha[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
    int main() {
    int num;
    cin >> num;
    if (num == 3) { //all 3 letter words
        for (int i = 0; i <= 25; i++) {
            for (int o = 0; o <= 25; o++) {
                for (int p = 0; p <= 25; p++) {
                    cout << alpha[i] << alpha[o] << alpha[p] << " ";
                }
            }
        }
    }
    else if (num == 4) { //all 4 letter words
        for (int i = 0; i <= 25; i++) {
            for (int o = 0; o <= 25; o++) {
                for (int p = 0; p <= 25; p++) {
                    for (int q = 0; q <= 25; q++) {
                        cout << alpha[i] << alpha[o] << alpha[p] << alpha[q] << " ";
                    }
                }
            }
        }
    }
    else {
        cout << "Not more than 4"; //it will take more than 2 hours for generating all 5 letter words
      }
    }
    

提交回复
热议问题