数据结构练习(21)字符串的排列
http://zhedahht.blog.163.com/blog/static/254111742007499363479/
很经典的题目,考察对递归的理解。
#include <cstdio> #include <cstring> void swap(char* ch1, char* ch2) { if (ch1 && ch2) { char ch = *ch1; *ch1 = *ch2; *ch2 = ch; } } void permutation(char* str, char* begin) { if (str && begin) { if (*begin == '\0') printf("%s\n", str); else { for (char *p = begin; *p != '\0'; ++p) { swap(begin, p); permutation(str, begin + 1); swap(begin, p); } } } } void permutation(char *str) { if (str) permutation(str, str); } int main() { char b[100]; while (scanf("%s", b) != EOF) permutation(b); return 0; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------