题目:从图中,我们可以发现26个字母分布在2-9这8个数字键上。某人的电话号码是65967427,观察单词“olympics”,可以发现:字母o位于数字键6上,字母l位于数字键5上,… 字母s 位于数字键7上。此时,我们说olympics是65967427对应的一个字母组合。65967427还可以对应其它很多种字母组合,例如:mjwmpgap也是其中之一。以C语言编写函数 :int transfer(int number)。该函数的输入为一个电话号码(允许输入为3位到11位的十进制数),在屏幕上输出该数字对应的所有字母组合,并返回组合的总数。如果数字中包含1或者0,由于没有与之对应的字母,则直接返回0。当数字小于3位或者大于11位时,亦没有对应字母组合,返回0。
package com.cric.telephone;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class TelephoneToWord {
/**
* @param args
*/
public static void main(String[] args) {
// 数字键与英文单词的对应字典word
char number1[] = { '1' };
char number2[] = { 'A', 'B', 'C' };
char number3[] = { 'D', 'E', 'F' };
char number4[] = { 'G', 'H', 'I' };
char number5[] = { 'J', 'K', 'L' };
char number6[] = { 'M', 'N', 'O' };
char number7[] = { 'P', 'Q', 'R', 'S' };
char number8[] = { 'T', 'U', 'V' };
char number9[] = { 'W', 'X', 'Y', 'Z' };
char number0[] = { '0' };
// 将Word存入HashMap
Map
wordmap.put(1, number1);
wordmap.put(2, number2);
wordmap.put(3, number3);
wordmap.put(4, number4);
wordmap.put(5, number5);
wordmap.put(6, number6);
wordmap.put(7, number7);
wordmap.put(8, number8);
wordmap.put(9, number9);
wordmap.put(0, number0);
System.out.println((char[]) wordmap.get(5));
System.out.println("Input A Telephon Number:");
Scanner input = new Scanner(System.in);
long telephonenumber = input.nextLong();
ArrayList
wordmap);
int listsize = arraytelephone.size();
switch (listsize) {
case 7:
buildTelephoneNumber7(arraytelephone, listsize);
break;
case 8:
buildTelephoneNumber8(arraytelephone, listsize);
break;
case 11:
buildTelephoneNumber11(arraytelephone, listsize);
break;
}
}
private static void buildTelephoneNumber11(
ArrayList
char english[] = new char[listsize];
for (int i = 0; i < arraytelephone.get(0).length; i++) {
english[0] = arraytelephone.get(0)[i];
for (int i1 = 0; i1 < arraytelephone.get(1).length; i1++) {
english[1] = arraytelephone.get(1)[i1];
for (int i2 = 0; i2 < arraytelephone.get(2).length; i2++) {
english[2] = arraytelephone.get(2)[i2];
for (int i3 = 0; i3 < arraytelephone.get(3).length; i3++) {
english[3] = arraytelephone.get(3)[i3];
for (int i4 = 0; i4 < arraytelephone.get(4).length; i4++) {
english[4] = arraytelephone.get(4)[i4];
for (int i5 = 0; i5 < arraytelephone.get(5).length; i5++) {
english[5] = arraytelephone.get(5)[i5];
for (int i6 = 0; i6 < arraytelephone.get(6).length; i6++) {
english[6] = arraytelephone.get(6)[i6];
for (int i7 = 0; i7 < arraytelephone.get(7).length; i7++) {
english[7] = arraytelephone.get(7)[i7];
for (int i8 = 0; i8 < arraytelephone
.get(8).length; i8++) {
english[8] = arraytelephone.get(8)[i8];
for (int i9 = 0; i9 < arraytelephone
.get(9).length; i9++) {
english[9] = arraytelephone
.get(9)[i9];
for (int i10 = 0; i10 < arraytelephone
.get(10).length; i10++) {
english[10] = arraytelephone
.get(10)[i10];
String str = String
.valueOf(english);
System.out.println(str);
}
}
}
}
}
}
}
}
}
}
}
}
private static void buildTelephoneNumber8(ArrayList
int listsize) {
char english[] = new char[listsize];
for (int i = 0; i < arraytelephone.get(0).length; i++) {
english[0] = arraytelephone.get(0)[i];
for (int i1 = 0; i1 < arraytelephone.get(1).length; i1++) {
english[1] = arraytelephone.get(1)[i1];
for (int i2 = 0; i2 < arraytelephone.get(2).length; i2++) {
english[2] = arraytelephone.get(2)[i2];
for (int i3 = 0; i3 < arraytelephone.get(3).length; i3++) {
english[3] = arraytelephone.get(3)[i3];
for (int i4 = 0; i4 < arraytelephone.get(4).length; i4++) {
english[4] = arraytelephone.get(4)[i4];
for (int i5 = 0; i5 < arraytelephone.get(5).length; i5++) {
english[5] = arraytelephone.get(5)[i5];
for (int i6 = 0; i6 < arraytelephone.get(6).length; i6++) {
english[6] = arraytelephone.get(6)[i6];
for (int i7 = 0; i7 < arraytelephone.get(7).length; i7++) {
english[7] = arraytelephone.get(7)[i7];
String str = String.valueOf(english);
System.out.println(str);
}
}
}
}
}
}
}
}
}
private static void buildTelephoneNumber7(ArrayList
int listsize) {
char english[] = new char[listsize];
for (int i = 0; i < arraytelephone.get(0).length; i++) {
english[0] = arraytelephone.get(0)[i];
for (int i1 = 0; i1 < arraytelephone.get(1).length; i1++) {
english[1] = arraytelephone.get(1)[i1];
for (int i2 = 0; i2 < arraytelephone.get(2).length; i2++) {
english[2] = arraytelephone.get(2)[i2];
for (int i3 = 0; i3 < arraytelephone.get(3).length; i3++) {
english[3] = arraytelephone.get(3)[i3];
for (int i4 = 0; i4 < arraytelephone.get(4).length; i4++) {
english[4] = arraytelephone.get(4)[i4];
for (int i5 = 0; i5 < arraytelephone.get(5).length; i5++) {
english[5] = arraytelephone.get(5)[i5];
for (int i6 = 0; i6 < arraytelephone.get(6).length; i6++) {
english[6] = arraytelephone.get(6)[i6];
String str = String.valueOf(english);
System.out.println(str);
}
}
}
}
}
}
}
}
public static ArrayList
Map
ArrayList
String str = String.valueOf(telephonenumber);
char ctelephonenumber[] = str.toCharArray();
if ((ctelephonenumber.length != 7) && (ctelephonenumber.length != 8)
&& (ctelephonenumber.length != 11)) {
System.out.println("wrong numbe!");
}
for (int i = 0; i < ctelephonenumber.length; i++) {
switch (ctelephonenumber[i]) {
case '1':
char number1[] = wordmap.get(1);
arraytelephone.add(i, number1);
break;
case '2':
char number2[] = wordmap.get(2);
arraytelephone.add(i, number2);
break;
case '3':
char number3[] = wordmap.get(3);
arraytelephone.add(i, number3);
break;
case '4':
char number4[] = wordmap.get(4);
arraytelephone.add(i, number4);
break;
case '5':
char number5[] = wordmap.get(5);
arraytelephone.add(i, number5);
break;
case '6':
char number6[] = wordmap.get(6);
arraytelephone.add(i, number6);
break;
case '7':
char number7[] = wordmap.get(7);
arraytelephone.add(i, number7);
break;
case '8':
char number8[] = wordmap.get(8);
arraytelephone.add(i, number8);
break;
case '9':
char number9[] = wordmap.get(9);
arraytelephone.add(i, number9);
break;
case '0':
char number0[] = wordmap.get(0);
arraytelephone.add(i, number0);
break;
}
}
return arraytelephone;
}
}