您好,我了解如何使用回圈迭代填充陣列。但是為了理解和彌合復雜性的差距,我想知道如何遞回地解決這個問題。
問題是讓 '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;
}
非常感謝,謝謝大家的回復!
uj5u.com熱心網友回復:
你可以試試這個:
static void fillArray(int[] nums, int index, int n)
{
if(index == nums.Length)
{
return;
}else
{
nums[index] = n;
fillArray(nums, index 1, n - 1);
}
}
static void Main(string[] args)
{
int n = 6;
int[] nums = new int[n];
fillArray(nums, 0, n);
for(int i = 0;i < nums.Length; i )
{
Console.WriteLine(nums[i]); //expected output 6,5,4,3,2,1
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/336521.html
上一篇:EFCoreDatabase.EnsureCreated獲取創建表SQL并且不向資料庫發送請求
下一篇:夾緊旋轉游戲物件
