全排列算法 一 递归求解

转帖自:http://blog.csdn.net/gaotong2055/article/details/8637745

集合R={1,2,3,4}的全排列

可以分解为:1,{2,3,4}的全排列 + 2,{1,3,4}的全排列 + 3,{1,2,4}的全排列 + 4,{1,2,3}的全排列。

继续分解:{2,3,4} 为 2,{3,4}的全排列,3,{2,4},  4,{2,3}………………………………

…………

直到集合里只有一个元素,就可直接输出了.

c语言代码:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int swap( char str[], int a, int b );
 6 int array( char str[], int start, int end );
 7 
 8 int main()
 9 {
10     char str[1000];
11     int len;
12 
13     while( 1 )
14     {
15         scanf( "%s", str );
16         len = strlen( str );
17 
18         array( str, 0, len );
19     }
20     return 0;
21 }
22 int array( char str[], int start, int end )
23 {
24     int i;
25     if( start == end - 1 )
26     {
27             printf( "%s\n", str );
28     }
29     else
30     {
31         for( i = start; i < end; i ++ )
32         {
33             swap( str, start, i );
34             array( str, start + 1, end );
35             swap( str, i, start );
36         }
37     }
38     return 0;
39 }
40 int swap( char str[], int a, int b )
41 {
42     char t;
43     t = str[a];
44     str[a] = str[b];
45     str[b] = t;
46     return 0;
47 }

 

 

 

转载于:https://www.cnblogs.com/hacker-hzh/archive/2013/03/10/2953125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值