图中第六题 C语言 用递归方法输出不同组合 急急急急!

如题所述

第1个回答  2014-05-08
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_SIZE (3)  //输出时一行有几个结果
#define INPUT_MAX (80) //输入数字时的最大长度

void meg(const char (* const nu)[3],char inp[],const int len,const int now) //nu是键盘对应的字符表,inp是输入的数字,len是inp的字符串长度,now调用时传入0
{
int i,t,j;
static char *mt=NULL;
static int cnt=0;
if (mt==NULL) mt=calloc(len+1,sizeof(char));
if (now==len)
{
printf("%s ",mt);
if (!(++cnt%LINE_SIZE)) putchar('\n');
}
else
for (i=0; i<3; i++)
{
mt[now]=nu[inp[now]-'0'][i];
meg(nu,inp,len,now+1);
if (isdigit(mt[now])) break;
}
}

int main(void)
{
char ltr[10][3]={{'0','0','0'},{'1','1','1'},{'A','B','C'},
{'D','E','F'},{'G','H','I'},{'J','K','L'},
{'M','N','O'},{'P','R','S'},{'T','U','V'},
{'W','X','Y'}};
char str[INPUT_MAX]="";
scanf("%[0-9]s",str);   //只读取行开头的连续数字

meg(ltr,str,strlen(str),0);
return 0;
}

相似回答