| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 427 人关注过本帖, 1 人收藏
标题:递归出所有的字符串,实在是不怎么会~写不出来了。。。希望各位指点一下啊
只看楼主 加入收藏
境善
Rank: 2
等 级:论坛游民
帖 子:76
专家分:16
注 册:2012-10-29
结帖率:86.21%
收藏(1)
已结贴  问题点数:1 回复次数:3 
递归出所有的字符串,实在是不怎么会~写不出来了。。。希望各位指点一下啊
题:2,3,4,5,6,7,8,9,所代表的字母
2: A,B,C     5: J,K,L    8: T,U,V
3: D,E,F     6: M,N,O    9: W,X,Y
4: G,H,I     7: P,R,S

例如:数字4734产生的字母序列是以下:

GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI
GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI
GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI
HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI
HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI
IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI
ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI

想用递归但是不怎么会写。。。


#include <stdio.h>
#include <string.h>
#define MAXSIZE 100
int count=0,i;
char *result[MAXSIZE];
char str[MAXSIZE];
char phoneKey[8][3]={{'A','B','C'},{'D','E','F'},
                    {'G','H','I'},{'J','K','L'},
                    {'M','N','O'},{'P','R','S'},
                    {'T','U','V'},{'W','X','Y'}};
//递归所有字符串
void allstring(int dig ,int num)
{
    if(num==-1)
        {
          str[4-num]='\0';
        strcpy(result[count],str);
        count++;
        return ;}
    else if(num>=0){
        str[4-num]=phoneKey[dig][i];  
           
    }
}


void main()
{
    int digits[4]={2,5,1,2};
allstring( ,4)


}
搜索更多相关主题的帖子: 字符串 字母 
2014-02-23 21:28
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:1 
全排列?

DO IT YOURSELF !
2014-02-23 21:44
境善
Rank: 2
等 级:论坛游民
帖 子:76
专家分:16
注 册:2012-10-29
收藏
得分:0 
回复 2楼 wp231957
随便输入长度大于1小于12的数字,有这些数字对应的字母所组成的所有的字符串,例如数字4734产生的字符串有:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI
GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI
GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI
HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI
HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI
IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI
ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
2014-02-24 08:48
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:1 
程序代码:
/**

 * 2,3,4,5,6,7,8,9,所代表的字母

 * 2: A,B,C     5: J,K,L    8: T,U,V

 * 3: D,E,F     6: M,N,O    9: W,X,Y

 * 4: G,H,I     7: P,R,S

 * 

 * 例如:数字4734产生的字母序列是以下:

 * 

 * GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI

 * GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI

 * GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI

 * HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI

 * HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI

 * IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI

 * ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
**/
#include <stdio.h>
#include <string.h>

#define  MAXLEN   20
#define  SIZE      3

const char key[][SIZE] = {
{'A','B','C'},{'D','E','F'},
{'G','H','I'},{'J','K','L'},
{'M','N','O'},{'P','R','S'},
{'T','U','V'},{'W','X','Y'}};

void foo(char *str, int step, char *digit)
{
    int i;

    if ('\0' == digit[step])
    {
        printf_s("%s ", str);
        return;
    }

    for (i = 0;i < SIZE;i += 1)
    {
        str[step] = key[digit[step] - '2'][i];
        foo(str, step + 1, digit);
    }
}

int main()
{
    char str[MAXLEN]   = {'\0'};
    char digit[MAXLEN] = {'\0'};

    gets_s(digit, MAXLEN);
    foo(str, 0, digit);

    return 0;
}


[fly]存在即是合理[/fly]
2014-02-25 23:19
快速回复:递归出所有的字符串,实在是不怎么会~写不出来了。。。希望各位指点一 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030382 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved