您的位置:首页 > 其它

用回溯法找出 n 个自然数中取 r 个数的全排列

2009-05-07 09:47 176 查看
using System;

using System.Collections.Generic;

using System.Text;

namespace ExArrange

{

class Arrange

{

public void Arrange(int n, int r)

{

int i = 0, j;

string s;

int[] a = new int
;

a[i] = 1;

while (true)

{

if ((a[i] - i) <= (n - r + 1))

{

if (i == (r - 1))

{

s = "";

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

{

s = s + Convert.ToString(a[j]) + ",";

}

// Memo1.Lines.Append(Trim(s));

Console.WriteLine(s);

a[i] = a[i] + 1;

continue;

}

i = i + 1;

a[i] = a[i - 1] + 1;

}

else

{

if (i == 0)

{

break;

}

i = i - 1;

a[i] = a[i] + 1;

}

}

}

static void Main(string[] args)

{

Ex03_19 e = new Ex03_19();

e.Arrange(6, 3);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: