标签:io for amp return on 递归 print class
/* 全排列的非递归实现,支持去掉重复 */ void main() { rbuf<int> v #v.push(1,2,3,4) for v.join('').printl ifn next_permutation<int>(v) break } bool next_permutation<T>(rbuf<T>& v) { if v.count<=1 return false next=v.count-1 for temp=next next-- if v[next]<v[temp] mid=v.count-1 for !(v[next]<v[mid]) mid-- swap<T>(v[next],v[mid]) reverse<T>(v,temp) return true if next==0 reverse<T>(v,0) return false } void swap<T>(T& a,T& b) { temp=a a=b b=temp } void reverse<T>(rbuf<T>& v,int begin,int end=v.count) { end-- for begin<end swap<T>(v[begin],v[end]) begin++ end-- }
标签:io for amp return on 递归 print class
原文地址:http://blog.csdn.net/roundsheep/article/details/38407775