C语言-递归做字符串数组元素全排列

本文探讨如何使用C语言通过递归算法实现字符串数组的所有元素全排列。内容包括递归的基本原理,以及将该原理应用于字符串数组排列的详细步骤和示例代码,帮助读者理解并掌握递归在字符串操作中的应用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <string.h>
void swap(char *a, char *b)		{	char tmp;	tmp = *a;	*a = *b;	*b = tmp;	}
int count = 0;
void permutation(char *list, int start, int end, int deep)
{
	printf("start=%d,end=%d, deep=%d into \n", start, end, deep);
	for (int i = start; i <= end; i ++)
	{
		swap(&list[i], &list[start]);
		permutation(list, start + 1, end, deep+1);
		swap(&list[i], &list[start]);
		if(i==end)
		{
			count++;
			printf("%s \n", list);
		}
	}
	printf("start=%d,end=%d, deep=%d out\n", start, end, deep);
}
int main()
{
	char list[]="abc";
	permutation(list, 0, 2, 0);
	printf("%d \n", count);
}
start=0,end=2, deep=0 into 
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
abc 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
acb 
start=2,end=2, deep=2 out
abc 
start=1,end=2, deep=1 out
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
bac 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
bca 
start=2,end=2, deep=2 out
bac 
start=1,end=2, deep=1 out
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
cba 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
cab 
start=2,end=2, deep=2 out
cba 
start=1,end=2, deep=1 out
abc 
start=0,end=2, deep=0 out
10 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值