题目描述:
实现代码:
public class RecursiveSearch {
static char c[][] = { // 该数组表示0~9分表表示的字母
{ 0 }, // 0 0表示空字符
{ 0 }, // 1
{ 'A', 'B', 'C' }, // 2
{ 'D', 'E', 'F' }, // 3
{ 'G', 'H', 'I' }, // 4
{ 'J', 'K', 'L' }, // 5
{ 'M', 'N', 'O' }, // 6
{ 'P', 'Q', 'R', 'S' }, // 7
{ 'T', 'U', 'V' }, // 8
{ 'W', 'X', 'Y', 'Z' } // 9
};
static int total[] = { 0, 0, 3, 3, 3, 3, 3, 4, 3, 4 }; //分别表示0~9所代表的字符的个数
public static void main(String[] args) {
// TODO Auto-generated method stub
int number[] = { 2, 3, 4 }; //假设电话号码为234,number[i]表示二维数组中的行值
int n = number.length;
int answer[] = new int[n]; //answer[i]表示二维数组c[i]中的列值
// 综上所述,当 answer[0]=2时 c[number[0]] [answer[0]]= c[2][2]='C'
recursiveSearch(number, answer, 0, n);
}
private static void recursiveSearch(int[] number, int[] answer, int index, int n) {
// TODO Auto-generated method stub
if (index == n) {
for (int i = 0; i < n; i++) {
System.out.printf("%c", c[number[i]][answer[i]]);
}
System.out.println();
return;
}
for (answer[index] = 0; answer[index] < total[number[index]]; answer[index]++) {
recursiveSearch(number, answer, index + 1, n);
}
}
}
测试结果: