我是靠谱客的博主 强健雨,这篇文章主要介绍写一个函数,输出字符串中字符的所有排序。(比如:abc acb bac bca cab cba ),现在分享给大家,希望可以做个参考。

方法1:

#include <stdio.h>
#include <stdlib.h>


 void Permutation(char a[], int start, int end)
{
int i,j;
char temp;
if(start == end)
{
for(i = 0; i <= end; i++)
printf(" %c ",a[i]);
printf("n");
}
else
{
for(i = start; i <= end; i++)
{
for(j=start;j<i;j++)
if(a[j]==a[i]) goto nextI;
temp=a[start]; a[start]=a[i]; a[i]=temp;
Permutation(a, start+1, end);
temp=a[start]; a[start]=a[i]; a[i]=temp;
nextI:;
}
}
}


int main()
{
char A[] = "abcc";
Permutation(A,0,3);


system("pause");
return 0;
}


方法2:

复制代码
1
一楼的思维复杂度比较高,这里给出一个简单的,如果字符串中各字符无重复且数量不多的情况下,可以采用穷举:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
复制代码
char ch[3] = {'a','b','c'};
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 3; k++)
{
if(i!=j&&i!=k&&j!=k)
print("%c,%c,%c",ch[i],ch[j],ch[k]);
} 


最后

以上就是强健雨最近收集整理的关于写一个函数,输出字符串中字符的所有排序。(比如:abc acb bac bca cab cba )的全部内容,更多相关写一个函数,输出字符串中字符的所有排序。(比如:abc内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部