经典的递归题。一层递归确定一个字母,第一层确定第一个字母。而确定字母是通过字符交换来实现排列的~~说的晦涩了一些,看代码吧:
#include
<
iostream
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
void
Permulate(
char
s[],
int
start,
int
all);
void
Copy(
char
a[],
char
b[],
int
i);
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
char s[10];
cin>>s;
int temp=0;
while(s[temp]) temp++;
Permulate(s,0,temp);
return 0;
}
void
Copy(
char
a[],
char
b[],
int
i)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
for(int m=0;m<i;m++)
b[m]=a[m];
}
void
Permulate(
char
s[],
int
start,
int
all)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int temp=0;
if(start==all-1)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<s[0];
for(temp=1;temp<all;temp++) cout<<' '<<s[temp];
cout<<endl;
return;
}
char a[10];
for(temp=start;temp<all;temp++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char chr=s[temp];s[temp]=s[start];s[start]=chr;
Copy(s,a,all);
Permulate(a,start+1,all);
}
}