编程之美3.2——电话号码对应英语单词

#include<iostream>  
using namespace std;  
const int MaxLength = 3;  
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};  
int number[MaxLength] = {2,3,4}; //本例输入数字2,3,4  
int answer[MaxLength] = {0};  
int len = 3;  
//排列树问题
//fun1和递归的方法是相反的
void fun1()
{
	while(true){  
        for(int i = 0; i < len; i++)  
            printf("%c", c[number[i]][answer[i]]);  
        printf("\n");  
          
        int k = len - 1;  
        while(k >= 0){  
            if(answer[k] < total[number[k]] - 1){  
                answer[k]++;  
                break;  
            }  
            else{  
                answer[k] = 0;  
                k--;  
            }  
        }  
        if(k < 0)  
            break;  
    }  
}
//递归
void RecursiveSearch(int index,int n)
{
	if(index==n)
	{
		for(int i=0;i<n;i++)
			cout<<c[number[i]][answer[i]];
		cout<<endl;
		return;
	}
	for(answer[index]=0;answer[index]<total[number[index]];answer[index]++)
	{
		RecursiveSearch(index+1,n);
	}
}
int main()  
{   
	//RecursiveSearch(0,MaxLength);
	fun1();
    return 0;  
}  

    原文作者:kereus
    原文地址: https://blog.csdn.net/arashif/article/details/45080779
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞