电话号码对应英语单词

题目描述:



实现代码:

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);
		}
	}

}

测试结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值