Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4733789
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-07-22 10:28:06

输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符abc所能排列出来的所有字符串abcacbbacbcacabcba

分析:这是一道很好的考查对递归理解的编程题,因此在过去一年中频繁出现在各大公司的面试、笔试题中。

我们以三个字符abc为例来分析一下求字符串排列的过程。首先我们固定第一个字符a,求后面两个字符bc的排列。当两个字符bc的排列求好之后,我们把第一个字符a和后面的b交换,得到bac,接着我们固定第一个字符b,求后面两个字符ac的排列。现在是把c放到第一位置的时候了。记住前面我们已经把原先的第一个字符a和后面的b做了交换,为了保证这次c仍然是和原先处在第一位置的a交换,我们在拿c和第一个字符交换之前,先要把ba交换回来。在交换ba之后,再拿c和处在第一位置的a进行交换,得到cba。我们再次固定第一个字符c,求后面两个字符ba的排列。

既然我们已经知道怎么求三个字符的排列,那么固定第一个字符之后求后面两个字符的排列,就是典型的递归思路了。

 
 
 

#include <stdio.h>
#include <stdlib.h>

void swap(char* a, char* b)
{
   char tmp = *a;
   *a = *b;
   *b = tmp;
}

void permutate(char* str, int begin, int end)
{
  int i;
  if(begin == end)
    printf("%s\n",str);
  else
   {
     for(i=begin; i<end; i++)
      {
        swap(str+begin,str+i);
        permutate(str, begin+1, end);
        swap(str+begin,str+i);
      }
   }
}
int main(int argc, char *argv[])
{
  char str[] = "abc";
  int len = strlen(str);
  permutate(str, 0, len);
  system("PAUSE");    
  return 0;
}

阅读(4262) | 评论(17) | 转发(0) |
0

上一篇:求数的深度

下一篇:从尾到头输出链表

给主人留下些什么吧!~~

chinaunix网友2010-07-09 16:40:26

http://www.yn-led.com.cn led桃花树 http://www.yn-led.com.cn led日光管 http://www.yn-led.com.cn led樱花树 http://www.zsqxled.com led樱花树 http://www.zsqxled.com led桃花树 http://www.zsqxled.com led桃花树灯 http://www.zsqxled.com led树灯 http://www.zsqxled.com led枫叶树 http://www.zsqxled.com led枫叶树灯 http://www.zsweiquan.com中山橡塑发泡 http://www.zsweiquan.com中山发泡 http://www.zsweiquan.com橡塑发泡 http://www.zsweiquan.com发泡 http://www.zsaml.com中山清洁公司 http://www.zsaml.com小榄清洁公司 http://www.zsaml.com中山保洁公司 http://w

chinaunix网友2010-07-09 16:40:05

http://www.zsanyao.com压克力 http://www.zsanyao.com中山压克力 http://www.zsanyao.com中山有机玻璃 http://www.cannytop.com淋浴房 http://www.cannytop.com简易淋浴房 http://www.cannytop.com中山淋浴房 http://www.cannytop.com广东淋浴房 http://www.marsbath.com淋浴房 http://www.marsbath.com简易淋浴房 http://www.marsbath.com中山淋浴房 http://www.marsbath.com广东淋浴房 http://www.china-snuggle.com淋浴房 http://www.china-snuggle.com简易淋浴房 http://www.china-snuggle.com中山淋浴房 http://www.china-snuggle.com广东淋浴房 http://www.zslshb.com中山环保 http://www.zs

chinaunix网友2010-07-06 11:28:30

[url=http:// www.zsyihua.com.cn]防水插头[/url] [url=http:// www.zsyihua.com.cn]防水接头[/url] [url=http:// www.zsyihua.com.cn]led护栏管配件[/url] [url=http://www.tof.cc]客房灯[/url] [url=http://www.tof.cc]水晶灯[/url] [url=http://www.tof.cc]现代灯[/url] [url=http://www.zsboke.com]中山篮球架[/url] [url=http://www.zsboke.com]珠海篮球架[/url] [url=http://www.zsboke.com]佛山篮球架[/url] [url=http://www.zsboke.com]江门篮球架[/url] [url=http://www.zsboke.com]中山健身路径[/url] [url=http://www.zsboke.com]江门健身路径[/url] [url=http://www

chinaunix网友2010-07-06 11:28:05

[url=http://www.zsanyao.com]压克力[/url] [url=http://www.zsanyao.com]中山压克力[/url] [url=http://www.zsanyao.com]中山有机玻璃[/url] [url=http://www.cannytop.com]淋浴房[/url] [url=http://www.cannytop.com]简易淋浴房[/url] [url=http://www.cannytop.com]中山淋浴房[/url] [url=http://www.cannytop.com]广东淋浴房[/url] [url=http://www.marsbath.com]淋浴房[/url] [url=http://www.marsbath.com]简易淋浴房[/url] [url=http://www.marsbath.com]中山淋浴房[/url] [url=http://www.marsbath.com]广东淋浴房[/url] [url=http://www.china-snuggle.com]淋浴房[/url]

chinaunix网友2010-07-05 16:59:26

[url=http://www.yglock.com]电插锁[/url] [url=http://www.yglock.com]电控锁[/url] [url=http://www.yglock.com]灵性锁[/url] [url=http://www.yglock.com]智能锁[/url] [url=http://www.yglock.com]一卡通锁[/url] [url=http://www.yglock.com]电子锁[/url] [url=http://www.yglock.com]感应卡锁[/url] [url=http://www.yglock.com]酒店锁[/url] [url=http://www.juxianglamp.com]酒店工程灯[/url] [url=http://www.juxianglamp.com]客房灯[/url] [url=http://www.juxianglamp.com]树脂灯[/url] [url=http://www.juxianglamp.com]巨祥灯[/url] [url=http