九键字母组合

 

Description

在手机九键里按下某几个数字会得到哪些字母组合?

例如,按下“23”的可能字母组合有9种 : ad ae af bd be bf cd ce cf

请按字典序输出所有的组合情况

Input

第一行输入N 代表输入的数字个数(1 ≤ N ≤ 4)

第二行N个数字,每个数字在2~9之间,每两个数字之间空格隔开

Output

按字典序输出全部可能的字母组合,每个可能的组合占一行

Sample Input

2
3 2

Sample Output

da
db
dc
ea
eb
ec
fa
fb
fc



 1 #include<iostream>
 2 using namespace std;
 3 int n;
 4 int a[103];
 5 string s;
 6 char p[103][103]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 7 void dfs(int x,string s){
 8     if(x==n+1){//如果x==n+1,就输出s 
 9         cout<<s<<endl;
10         return;
11     }
12     int num=(a[x]==7||a[x]==9)?4:3;//数字7和9键上有四个字母 ,其他键有三个 
13     for(int i=0;i<num;i++){//遍历每个字母 
14         dfs(x+1,s+p[a[x]][i]);
15     }
16 }
17 int main(){
18     scanf("%d",&n);
19     for(int i=1;i<=n;i++){
20         scanf("%d",&a[i]);
21     }
22     dfs(1,s);//开始搜索 
23     return 0;
24 }

 

用深度优先搜索,




原文地址:https://www.cnblogs.com/fate-/p/12322031.html