如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!种。如:给定A,B,C三个不同的字符,则输出:ABC,ACB,BAC,BCA,CAB,CBA
#include <stdio.h>
#include <stdlib.h>typedef struct node
{
int location;
int isUsed;
}Node;
void swap(Node *a,Node *b)
{
Node temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int n;
char str[27]={'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'};
scanf("%d",&n);
Node node[n];
for(int i=0;i<n;i++)
{
node[i].isUsed = 0;
node[i].location = i;
}
for(int i=0;i<n;i++)
{
if(node[i].isUsed==0)
{
for(int j=node[i].location+1;j<n;j++)
{
swap(&node[i],&node[j]);
for(int i=0;i<n;i++)
{
// printf("%d\n",node[i].location);
printf("%c",str[node[i].location]);
}
printf("\n");
}
}
}
getchar();
getchar();
//printf("%d",node[0].location);
}
文章由http://www.400815.com原创,转载请注明。