How to generate all the different values ​​when converting a number to a phone vanity letter in C ++

the purpose of C ++, in which it works, is to ask the user for a 4-digit number, and then the program will display all the possible fuss letters based on the telephone keypad. Currently, I am sticking to the best way to get a program to work and how to write it. Does anyone have any idea? thanks for the help!

+3
source share
4 answers

Sounds homework, so I’ll just give you an idea:

You need to match each number with the corresponding set of letters. Then you create possible permutations for all the different letters corresponding to the corresponding numbers.

, "1" : "A", "B" "C", "2" - "D", "E" "F", "12"

AD
AE
AF
BD
BE
BF
CD
CE
CF

?

+2

- , . , ?

+1

++ , . . , .

- , , - :)

std::map<char,std::string> letters = new std::map<char,std::string>();
letters.put('1', "ABC");
letters.put('2', "DEF");
... etc ...

std::vector<std::string> all_permutations = new std::vector<std::string>();

void GeneratePermutations( std::string number, int digit, std::string vanity) {
   std::string letters_for_number = letters.get(number[digit - 1]);

   for( int i = 0; i < letters_for_number.size(); i++ ) {
      vanity += letters_for_number[i];
      if ( digit < number.length() ) {
         GeneratePermutations(number, digit + 1, vanity);
      } else {
         all_permutations.add(vanity);
      }
   }
}

void main() {
    GeneratePermutations("1234", 1, "");
}
0

:

main(char[] input) {
  for(int i = 0 .. 3) { letters[i] = letters(input[i]) };

  for(int i = 0 .. letters[0].length) {
    for(int j = 0 .. letters[1].length) {
      for(int k = 0 .. letters[2].length) {
        for(int l = 0 .. letters[3].length) {
          print(letters[0][i], letters[1][j], letters[2][k], letters[3][l]);
        }
      }
    }
  }
}

char[] letters(char digit) {
  switch (digit) {
    case '2' : {'a', 'b', 'c'};
    ..
    case '9' : {'w', 'x', 'y', 'z'};
  }
}
-1

Source: https://habr.com/ru/post/1719633/


All Articles