using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 递归反转数组
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 5, 34, 7, 51 };//初始int数组
ReverseArray(nums, 0,nums.Length-1);//调用反转函数
//输出结果
foreach (int i in nums)
{
Console.Write(i + " ");
}
Console.ReadKey();
}
/// <summary>
/// ReverseArray()反转int[]数组
/// </summary>
/// <param name="nums"></param>
/// <param name="startIndex">startIndex是int[]的你要反转的部分的起始索引</param>
/// <param name="endIndex">endIndex是int[]的你要反转的部分的终止索引</param>
public static void ReverseArray(int[] nums,int startIndex,int endIndex)
{
if (startIndex >= endIndex)
{
return;
}
int temp = nums[startIndex];
nums[startIndex] = nums[endIndex];
nums[endIndex] = temp;
ReverseArray(nums, startIndex + 1, endIndex - 1);//递归调用
}
}
}
这样也可以 |