permutation求全排列

include <iostream>
#include <string>

using namespace std;


void swap(char &c1, char &c2){
    char tmp = c1;
    c1= c2;
    c2= tmp;
}

void perm(string s, int i, int n){

    if(i== n){
        cout << s << endl;
    }

    for(int j=i;j<=n;j++){
        swap(s[i], s[j]);
        perm(s, i+1, n);
        swap(s[i], s[j]);
    }

}



int main(){

    string p= "1234";
    perm(p, 0, 3);


}

 

posted @ 2014-12-25 13:13  zmiao  阅读(124)  评论(0编辑  收藏  举报