# C#递归算法和排列算法

## 一、递归算法

### 2、实例：

```static void  Main(string[] args)
{
int[] sum = new int[30];
for (int i = 0; i < sum.Length; i++)
{
sum[i] = process1(i);
Console.WriteLine(sum[i]);
}
}
public static int process1(int a)
{
if (a == 0 || a == 1) return 1;

return process1(a - 1) + process1(a - 2);
}```

### 3、阶乘算法：

```public static int process2(int n)
{
if (n == 1) return 1;

return n * process2(n - 1); // 相同重复逻辑，缩小问题的规模
}```

## 二、排列算法

```      public static void Permutation(string[] nums, int m, int n)
{
string t;
if (m < n - 1)
{
Permutation(nums, m + 1, n);
for (int i = m + 1; i < n; i++)
{
//可抽取Swap方法
t = nums[m];
nums[m] = nums[i];
nums[i] = t;
Permutation(nums, m + 1, n);

//可抽取Swap方法
t = nums[m];
nums[m] = nums[i];
nums[i] = t;
}
}
else
{
#region 存放到List
Node root = null;
Node currentNode;
for (int j = 0; j < nums.Length; j++)
{
currentNode = new Node(nums[j]);
currentNode.nextNode = root;
root = currentNode;
}
#endregion

#region  打印控制台
for (int j = 0; j < nums.Length; j++)
{
Console.Write(nums[j]);
}
Console.WriteLine();

#endregion
}
}```

```      static void Main(string[] args)
{
Nums = new string[] { "a", "b", "c" };
Permutation(Nums, 0, Nums.Length);