比这篇新的文章: StrCombination
比这篇旧的文章: ReverseWords

StrPermutation

语言: C, 标签: 无  2008/08/27发布 2个月前更新
作者: bookine, 点击303次, 评论(1), 收藏者(0)

开关行号, 全选(Ctrl+C复制) | 一键复制:HTML, BBCode(Discuz!) , 源代码 | 查看:裸代码, 全屏
背景
主题: 字体:
C语言: StrPermutation
01 //print the whole permutation of a string
02 void StrPermutation(char str[], int start, int end)
03 {
04     int i = 0;
05     char tmp;
06
07     //this is the end of the recursion
08     if(start == end)
09     {
10         for(i = 0; i <= end; ++ i)
11             printf("%c", str[i]);
12         printf("\n");
13     }
14     else
15     {
16         //use the original sub-string to do recursion
17         StrPermutation(str, start + 1, end);
18         for(i = start + 1; i <= end; ++ i)
19         {
20             //exchange characters
21             tmp = str[start]; str[start] = str[i]; str[i] = tmp;
22             //use the new sub-string to do recursion
23             StrPermutation(str, start + 1, end);
24             //restore the string
25             tmp = str[start]; str[start] = str[i]; str[i] = tmp;
26         }
27     }
28 }
打分:

所有评论,共1条:( 我也来说两句)

1
半瓶墨水 2个月前 回复
0
0
全排列?
如果是Combination的话,至今为止我见过的最nx的是这样做的:
Q: “abcde”的组合
A: 00000,五个二进制bit,每次加1,直到加为11111。每次加之前输出对应的位置上为1的字符

发表评论

注册登录后再发表评论