java全排列的递归算法_全排列的递归算法 - Feng.Li's Java See - BlogJava

void perm(char *list, int i, int n){

int j,temp;

if(i == n){

for(j = 0; j < n; j++)

cout<

cout<

}

else{

for(j = i; j < n; j++){

SWAP(list[i],list[j],temp);

perm(list,i+1,n);

SWAP(list[i],list[j],temp);

}

for(j = i; j < n; j++){

SWAP(list[i],list[j],temp); //将第j数字做为第一个

perm(list,i+1,n);

SWAP(list[i],list[j],temp); //将第j数字换回原来位置,准备用第j+1个做做为第一个

}

-----------------------------------------------------------

建议你将函数修改成以下形式,自己观察一下

-----------------------------------------------------------

void perm(char *list, int i, int n){

int j,temp;

for(j = 0; j < n; j++)

cout<

if(i == n){

for(j = 0; j < n; j++)

cout<

cout<

}

else{

for(j = i; j < n; j++){

SWAP(list[i],list[j],temp);

perm(list,i+1,n);

SWAP(list[i],list[j],temp);

}

========================================================================================================

下面是完整的程序:C#

using System;

using System.Collections.Generic;

using System.Text;

namespace Test

{

class Program

{

static void Main(string[] args)

{

char[] list = {'a','b','c','d' };

perm(list, 0, 4);

Console.ReadLine();

}

static void perm(char[] list, int i, int n)

{

int j,temp;

if(i==n)

{

for(j=0;j

{

Console.Write(list[j]);

//Console.WriteLine();

}

}

else

{

for (j=i;j

{

swap(ref list[i],ref list[j]);

perm(list,i+1,n);

swap(ref list[i], ref list[j]);

}

}

}

static void swap(ref char  a, ref char  b)

{

char c = a;

a = b;

b = c;

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值