编程之美:电话号码对应英文单词( 程序实现)

/************************************************************************/
/*
 实现:将输入的手机号码对应手机键盘的字母形成组合,并打印输出
 输入: 电话号码位数,对应的电话号码
 输出:电话号码对应所用字母组合
*/
/************************************************************************/
#include <cstdio>
#include <assert.h>
#include <cstring>

using namespace std;

char dict[10][5]={
    "",//0
    "",//1
    "ABC",//2
    "DEF",//3
    "GHI",//4
    "JKL",//5
    "MNO",//6
    "PQRS",//7
    "TUV",//8
    "WXYZ"//9
};

int total[10]={0,0,3,3,3,3,3,4,3,4};

void printAllStr(int * arr,int len);

int main(int argc,char * argv[])
{
 
 int telNum = 0;
 while(scanf("%d",&telNum)!=EOF)
 {
     int* arr = new int[telNum];
     if(!arr)
     {
	printf("failed to create new arr\n");
	break;
     }
     for(int i=0;i<telNum;++i)
     {
	    scanf("%d",arr+i);
     }
     // TODO print function
     printAllStr(arr,telNum);
     delete []arr;
 }
 return 0;
}

void printAllStr(int * arr, int len)
{
    int* res = new int[len];
    assert(res);   
    memset(res,0,sizeof(int)*len);
    
    int resMulty = 1;
    for(int i = 0; i< len ; ++i)
    {
	if(total[arr[i]])	   
	    resMulty *= total[arr[i]];
    }
    
    printf("\n resMulty=%d\n",resMulty);
    
    int changeI = 0;
    int  ordinary = 1;
    while(true)
    {
	printf("[%03d]:",ordinary);
	for(int i =0;i<len;++i)
	    printf("%c",dict[ arr[i] ][  res[i] ]);
	puts("");

	ordinary ++;
	changeI = 0;
	while(changeI<len)
	{	 
	     // 前一组的序号满了,则:先改变下标,再改变对应下标的输出序号 
	     if(res[ changeI  ]<total[ arr[changeI ] ]-1)
	     {
		res[ changeI ]++;
		break;
	     }
	     else
	     {
	    	res[ changeI ] = 0;
	    	changeI++;
	     }
        }
	 if(changeI == len)
	    break;
    }
    delete []res;   
    puts("\nend of one case");
}

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