如何将这种递归循环改为非递归?我知道这种方法很简单,但我对此解决方案的非递归方式感兴趣。
using System;
using System.IO;
namespace NonRecursion {
class NonRecursion {
static void Main() {
string createPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string getPath = createPath + @"\folder";
GetDirsPath(getPath);
Console.ReadKey();
}
static void GetDirsPath(string getPath) {
string[] dirs = Directory.GetDirectories(getPath);
for (int i = 0; i < dirs.Length; i++) {
Console.WriteLine(dirs[i]);
GetDirsPath(dirs[i]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以只更改这个功能吗?
static void GetDirsPath(string getPath) {
string[] dirs = Directory.GetDirectories(getPath);
for (int i = 0; i < dirs.Length; i++) {
Console.WriteLine(dirs[i]);
GetDirsPath(dirs[i]);
}
}
Run Code Online (Sandbox Code Playgroud) using System;
namespace G09 {
class Reverse {
static void Main()
{
Console.WriteLine(ReverseText(24));
Console.ReadKey();
}
static string ReverseText(int n) {
if (n < 1)
{
return "";
}
string index = "1";
string revIndex = "";
int count = 1;
string[] arr = new string[n];
for (int i = 0; i < n; i++)
{
arr[i] = index + revIndex;
for (int j = 0; j <= i; j++)
{
if (count > 8)
{
index = index + 0; …Run Code Online (Sandbox Code Playgroud)