其实就是搜索解答树的暴利搜索。
编程之美P222
#include <iostream>
using namespace std;
char c[10][10]={
"","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"
};
int total[10]={0,0,3,3,3,3,3,4,3,4};
void recursiveSearch(int number[],int answer[],int telLen,int cur){
if(cur==telLen){
for(int j=0;j<telLen;j++){
cout<<c[number[j]][answer[j]];
}
cout<<endl;
return;//需要return
}
for(int i=0;i<total[number[cur]];i++){
answer[cur]=i;
recursiveSearch(number,answer,telLen,cur+1);
}
if(total[number[cur]]==0){
answer[cur]=0;
recursiveSearch(number,answer,telLen,cur+1);
}
}
int main() {
int number[]={9,2,1};
int answer[10];
recursiveSearch(number,answer,3,0);
return 0;
}