- 论坛徽章:
- 6
|
本帖最后由 dorodaloo 于 2018-01-16 18:07 编辑
回复 1# xlwang_0903
我来写一个- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *key[] = {
- "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz",
- };
- void compose(char *keys);
- void cpsFunc(unsigned index, char *keys, char *composedStr);
- int main(int argc, char *argv[]) {
- char *test[] = {"001", "3", "02310", "23", "2b4", "357", "9925", "987654"};
- unsigned size = sizeof(test) / sizeof(char *);
- for (int i = 0; i < size; i++) {
- printf("input = %s\n", test[i]);
- compose(test[i]);
- printf("\n\n");
- }
- }
- void compose(char *keys) {
- unsigned size = strlen(keys);
- for (unsigned i = 0; i < size; i++)
- if (!isdigit(keys[i])) {
- fprintf(stderr, "'%c' NOT digit\n", keys[i]);
- return;
- }
- char composedStr[size + 1];
- memset(composedStr, 0, size + 1);
- cpsFunc(0, keys, composedStr);
- }
- void cpsFunc(unsigned index, char *keys, char *composedStr) {
- if (!*keys) {
- printf("%s ", composedStr);
- return;
- }
- if (!strlen(key[*keys - '0'])) {
- cpsFunc(index, keys + 1, composedStr);
- return;
- }
- for (char *alpha = key[*keys - '0']; *alpha; alpha++) {
- composedStr[index] = *alpha;
- cpsFunc(index + 1, keys + 1, composedStr);
- }
- }
复制代码
|
|