比这篇新的文章:
StrCombination
比这篇旧的文章: ReverseWords
作者: bookine, 点击303次, 评论(1), 收藏者(0)
打分:
所有评论,共1条:( 我也来说两句)
比这篇旧的文章: ReverseWords
StrPermutation
语言: C, 标签: 无 2008/08/27发布 2个月前更新作者: bookine, 点击303次, 评论(1), 收藏者(0)
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 }
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个月前
回复
全排列?
|
代码