当前位置:编程学习 > C/C++ >>

全排列

[cpp]  
#include <iostream>  
  
using namespace std;  
  
template < class Type >  
void Perm(Type list[], int k, int m)  
{  
    if (k == m)  
    {  
        for (int i=0; i<=m; ++i)  
        {  
            cout << list[i];  
        }  
        cout << endl;  
    }  
    else  
    {  
        for (int i=k; i<=m; ++i)  
        {  
            Swap(list[k],list[i]);  
            Perm(list,k+1,m);  
            Swap(list[k],list[i]);  
        }  
    }  
}  
  
template < class Type >  
inline void Swap(Type& a, Type& b)  
{  
    Type temp = a;  
    a = b;  
    b = temp;  
}  
  
int main()  
{  
    int list[] = {1,2,3};  
    Perm(list,0,2);  
  
    return 0;  
}  
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,