输出一个字符串的全排列

#include<iostream>
#include<cstring>
using namespace std;

void swap(char &a, char &b)
{
    char t = a;
    a = b;
    b = t;
}
void permutation(char ch[],int s, int e)
{
    int i = 0;
    if(s > e)
        return;
    if(s == e)
    {
        for(i=0; i<=e; i++)
            cout<<ch[i];
        cout<<endl;
        return;
    }
    for(i=s; i<=e; i++)
    {
        bool isExist = false;
        for(int j=i-1; j>=s; j--)
            if(ch[j] == ch[i])
            {
                isExist = true;
                break;
            }
        if(!isExist)
        {
            swap(ch[s],ch[i]);
            permutation(ch,s+1,e);
            swap(ch[s],ch[i]);
        }
    }
}

int main()
{
    char ch[100];
    cout<<"please input the string!"<<endl;
    cin>>ch;
    int len = strlen(ch);
    permutation(ch,0,len-1);
    cout<<""<<endl;
    return 0;
}

 

posted @ 2013-03-11 23:34  Frank@609  Views(314)  Comments(0Edit  收藏  举报