不知道写什么,把昨天写的一个全排列贴出来吧。8-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <algorithm> using namespace std; void Perm(char * s) { static char* st = s; if(!*(s+1)) { cout<<st<<endl; return ; } else { for(char *t = s; *t; ++t) { swap(*s, *t); Perm(s + 1); swap(*s, *t); } } } |
再来一个考虑串内有重复字符的,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <algorithm> #include <set> using namespace std; void Perm(char * s) { static char* st = s; if(!*(s+1)) { cout<<st<<endl; return ; } else { set<char> Set; for(char *t = s; *t; ++t) { size_t size = Set.size(); Set.insert(*t); if(size != Set.size()) { swap(*s, *t); Perm(s + 1); swap(*s, *t); } } } } |
你好!除了代码,此处没有多少原创之物,皆为本人搜集、整理、总结之记录与心得,欢迎转载分享!转载时请尽量注明出处,将不胜感激。祝你健康、快乐!
Be the first to comment on this entry.