【发布时间】:2021-12-11 10:12:59
【问题描述】:
您好,我了解如何用循环迭代填充数组。但是为了理解和弥合复杂性的差距,我想知道如何递归地解决这个问题。
问题是让'n'成为数组'nums'的索引。 nums 将所有以前的数字存储在它的数组中。例如,n=6; nums = [0=6],[1=5],[2=4],[3=3],[4=2],[5=1]。
static void Main(string[] args)
{
int n = 6;
int[] nums = new int[n];
for(int i = 0;i < nums.Length; i++)
{
Console.WriteLine(fillArray(nums, n)[i]); //expected output 6,5,4,3,2,1
}
}
static int[] fillArray(int[] nums, n)
{
for(int i = 0; i< nums.Length; i++)
{
nums[i] = n--;
}
return nums;
}
非常感谢,谢谢大家的回复!
【问题讨论】:
标签: c# arrays recursion data-structures