'commit'
This commit is contained in:
56
C++专题课程/全排列/AllPermutation.cpp
Normal file
56
C++专题课程/全排列/AllPermutation.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
//ȫ<><C8AB><EFBFBD>д<EFBFBD><D0B4><EFBFBD>˵<EFBFBD><CBB5>
|
||||
|
||||
// https://www.jianshu.com/p/e0de4c9b73f2?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
|
||||
|
||||
using namespace std;
|
||||
|
||||
//<2F><>[from, to]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>±<EFBFBD>Ϊfrom<6F><6D><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
bool IsSwap(char* from, char* to)
|
||||
{
|
||||
char* p;
|
||||
for(p = from; p < to; p++)
|
||||
{
|
||||
if(*p == *to)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AllPermutation(char* perm, int from, int to)
|
||||
{
|
||||
if(from > to)
|
||||
return;
|
||||
|
||||
if(from == to) //<2F><>ӡ<EFBFBD><D3A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
||||
{
|
||||
static int count = 1; //<2F>ֲ<EFBFBD><D6B2><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3>ȫ<EFBFBD><C8AB><EFBFBD>еĸ<D0B5><C4B8><EFBFBD>
|
||||
cout << count++ << ":" << perm;
|
||||
cout << endl;
|
||||
}
|
||||
if(from < to) //<2F>õݹ<C3B5>ʵ<EFBFBD><CAB5>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
|
||||
{
|
||||
for(int j = from; j <= to; j++) //<2F><>j<EFBFBD><6A><EFBFBD>ַ<EFBFBD><D6B7>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵõ<DCB5><C3B5>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
if(IsSwap((perm + j), (perm + to)))
|
||||
{
|
||||
swap(perm[j], perm[from]);
|
||||
//cout<<0;
|
||||
AllPermutation(perm, from + 1, to);
|
||||
//cout<<1;
|
||||
swap(perm[j], perm[from]);
|
||||
//cout<<2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char str[100];
|
||||
cin >> str;
|
||||
AllPermutation(str, 0, strlen(str) - 1);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user