码迷,mamicode.com
首页 > 其他好文 > 详细

全排列的实现

时间:2017-10-19 00:19:17      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:include   clu   tar   iss   std   ==   ios   swap   swa   

1.递归实现

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int sum=0;
int Isswap(char a[],int start,int end)
{
    for(int i=start;i<end;i++)
    {
        if(a[i]==a[end])
         return 0;
    }
     return 1;


}
void swap (char a[],int b,int c)
{

    char temp=a[b];
    a[b]=a[c];
    a[c]=temp;

}
void quanpai(char a[],int start ,int end)
{

    if (start==end)
    {
       for(int i=0;i<=end;i++)
           cout<<a[i];
        cout<<endl;
        sum++;
       return;
    }
    else
    {
        for(int j=start;j<=end;j++)
        {
            if(Isswap(a,start,j)) {

                swap(a, start, j);
                quanpai(a, start + 1, end);
                swap(a, j, start);
            }
        }
    }

}
int main()
{
      char zifu[100];
      cin >>zifu;
      int  n=strlen(zifu);
      quanpai(zifu,0,n-1);
      cout<<sum<<endl;


    return 0;
}

 

全排列的实现

标签:include   clu   tar   iss   std   ==   ios   swap   swa   

原文地址:http://www.cnblogs.com/2014slx/p/7689830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!