C语言基础
递归、迭代、循环、查找、排序
0001 求26个字母的全排列
That's a tough question but thankfully, our team is on it. Please bear with us while we're investigating.
#include <stdio.h>
#include <iostream>
using namespace std;
#define N 10
include
#include <stdio.h>
using namespace std;
int n;
char a[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
'x','y','z'};
int count=0;
void swap(int x,int y)
{
int tmp;
tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
void search(int k)
{
int i;
if(k == n)
{
for(i = 0;i < n;i++)
{
cout << a[i];
}
++count;
cout << endl;
}
else
{
for(i = k;i < n;i++)
{
swap(i,k);
search(k+1);
swap(i,k);
}
}
}
int main()
{
cin >> n;
search(0);
printf("共%d种",count);
return 0;
}
此题的思路很简单。对于一个不含重复元素的全排列,使用递归划分,第一次{a}search(0,0)自己与自己排列看成已排列好,第二次{a,b},search(c),以此类推,直到只剩一个,表示已经排好,然后从头开始排列{a,c},search(b)……
Have you had a chance to answer the previous question?
Yes, after a few months we finally found the answer. Sadly, Mike is on vacations right now so I'm afraid we are not able to provide the answer at this point.
Last updated