题目:列出九宫格键盘输入的所有字母组合
思路:将字母的组合看成k进制的数,列出字母组合的过程即为对数进行加法运算。
#include <stdio.h> #include <string.h> #include <stdlib.h> char* letter[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; void listAll(char* input) { int len = strlen(input); int total = 1; int i; for ( i=0; i<len; i++) { total = total*strlen(letter[input[i]-'0']); } for( i=0; i<total; i++) { //char item[4]; char* item = (char*)malloc(len+1); int j; for( j=0; j<len; j++) { int size = strlen(letter[input[j]-'0']); int num = i; int index = num%size; num = num/size; item[j] = letter[input[j]-'0'][index]; } item[j] = ' '; printf("%s ",item); } } int main() { char* input = "025"; listAll(input); return 0; }