class Permutation
{
public :
Permutation(const char* sour);
~Permutation();
void printPermutation(int start,int end);
private :
char * p_str;
void Swap(char &a,char &b);
};
#include "alla.h"
#include<string>
#include<iostream>
using namespace std;
Permutation::Permutation(const char *sour)
{
if(sour == NULL)
{
p_str = NULL;
}
cout<<"Permutation"<<endl;
int len = strlen(sour);
p_str = new char[len + 1];
int i = 0;
cout<<len<<endl;
while( sour[i] != '\0')
{
p_str[i] = sour[i];
i++;
}
p_str[i] = '\0';
}
Permutation::~Permutation()
{
if(p_str!=NULL)
delete [] p_str;
p_str = NULL;
cout<<"~~~~permutation"<<endl;
getchar();
}
void Permutation::printPermutation(int start,int end)
{
if(start == end-1)
{
for(int i = 0; i < end; i++)
cout<<p_str[i];
cout<<endl;
}
else
{
for(int i = start; i < end; i++)
{
Swap(p_str[i],p_str[start]);
printPermutation(start+1,end);//这里一定要是start +1 不 是i +1
Swap(p_str[i],p_str[start]);
}
}
}
void Permutation::Swap(char &a,char &b)
{
char c = a;
a = b;
b = c;
}
全排列
最新推荐文章于 2025-03-10 22:27:24 发布