Tag: 递归

如何使用C#打印1到100而不进行任何循环

我试图使用C#从1到100打印数字而不使用循环。 有什么线索吗?

递归是如何工作的

这是我的代码 using System; public class Program { public static void Method(int flowerInVase) { if (flowerInVase > 0) { Method(flowerInVase – 1); Console.WriteLine(flowerInVase); } } public static void Main() { Method(3); } } 我对行Console.WriteLine(flowerInVase);感兴趣Console.WriteLine(flowerInVase); 该方法调用自身直到它被条件终止。 只有在堆栈已满之后,它才会弹出上面的每个方法,并且控制台从最少1,2,3开始写入数字。 为什么console.writeline只在堆栈弹出时才起作用,为什么它不是在方法转到终止的方式上写数字,比如3,2,1? 编译器仅在完成递归时才使用writeline。

创建所有可能的数组而不嵌套for循环

我想生成所有可能的长度为n数字,并且我的数字的每个数字都有一个来自集合{1,2,…,n-1}作为数组。 换句话说,我想列出长度为n且不包括0所有基数n 。 现在,我能想到的唯一方法是嵌套n for循环,并使用第(i + 1)个循环分配myArray[i] ,即 int n; int[] myArray = new int[n]; for (int i1 = 1; i1 < n; i1++) myArray[0]=i1; for (int i2 = 1; i2 < n; i2++) myArray[1]=i2; // and so on…. for (int in = 1; in < n; in++) { myArray[n]=in; foreach (var item in myArray) […]

如何执行完整的递归目录和文件扫描?

这是我的代码: private static void TreeScan(string sDir) { foreach (string d in Directory.GetDirectories(sDir)) { foreach (string f in Directory.GetFiles(d)) { //Save file f } } TreeScan(d, client); } 问题是它没有获得sDir (起始目录)的文件,它只获取子文件夹中的文件夹和文件。 如何让它从sDir获取文件呢?

C# – entity framework – mscorlib.dll中发生未处理的“System.StackOverflowException”类型exception

mscorlib.dll中发生了未处理的“System.StackOverflowException”类型exception 确保没有无限循环或无限递归。 以下代码在此方法成功时调用: internal static List GetProductsSoldByCompany(Guid CompanyID) { var ret = from a in _dbRiv.ProductsSold where a.Company.CompanyId == CompanyID select a; return ret.ToList(); } 在返回时,它调用实体模型并尝试填充所有外键控对象(子对象)。 架构是[1公司有0到多个ProductsSold]。 出于某种原因,对以下代码的调用只会自行级联: [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute(“RIV_Model”, “FK_ProductsSold_Company”, “Company”)] [global::System.Xml.Serialization.XmlIgnoreAttribute()] [global::System.Xml.Serialization.SoapIgnoreAttribute()] [global::System.Runtime.Serialization.DataMemberAttribute()] public Company Company { get { return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference(“RIV_Model.FK_ProductsSold_Company”, “Company”).Value; } set { ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference(“RIV_Model.FK_ProductsSold_Company”, “Company”).Value = value; } } /// /// There […]

LINQ递归查询返回分层的组集

给出以下模型的列表 public class Team { public int TeamId { get; set; } public int ParentTeamId { get; set; } } 我正在尝试编写一个递归的linq查询,这将使我能够检索看起来像这样的层次结构 Team ChildTeams Team Team ChildTeams 我尝试了很多方法并看到了许多类似的问题,但没有一个能够帮助我解决问题。 我尝试的最新尝试遵循以下方针: private class TeamGrouping { public int? ParentTeamId { get; set; } public IEnumerable ChildTeams { get; set; } public IEnumerable Grouping { get; set; } } private […]

在C#中使用递归

在使用递归来避免堆栈溢出时是否有任何一般规则?

从DataTable填充WinForms TreeView

我有一个WinForm TreeView控件,显示CaseNotes的父子关系(我知道这对大多数人来说没什么意义,但它可以帮助我看到答案)。 我有一个我需要显示的CaseNotes的DataTable。 父/子被定义为:如果行具有ParentNoteID,则它是该注释的childNode,否则它是rootNode。 它也可以是父笔记(但不是rootNode),如果另一行有它的ID,因为它是ParentNoteID。 为了使事情变得复杂(可能是简化),我有以下工作(大部分)代码,它们交替地为节点着色。 我手动为树视图创建了一个静态集合,它可以非常正确地为它们着色。 现在我需要从我的DataTable动态填充节点。 既然我已经逐个节点地通过树视图,那么我不能以某种方式将数据附加到这个过程中吗? 也许我需要先构建节点,然后将颜色作为一个单独的例程,但递归方法仍然适用,对吗? 假设我想为每个节点显示CaseNoteID。 这是在DataTable中返回的,并且是唯一的。 foreach (TreeNode rootNode in tvwCaseNotes.Nodes) { ColorNodes(rootNode, Color.MediumVioletRed, Color.DodgerBlue); } protected void ColorNodes(TreeNode root, Color firstColor, Color secondColor) { root.ForeColor = root.Index % 2 == 0 ? firstColor : secondColor; foreach (TreeNode childNode in root.Nodes) { Color nextColor = childNode.ForeColor = childNode.Index % […]

将有向无环图(DAG)转换为树

我正在尝试实现algoritm将Directed Acyclic Graph转换为Tree(为了好玩,学习,kata,命名它)。 所以我想出了数据结构Node: /// /// Represeting a node in DAG or Tree /// /// Value of the node public class Node { /// /// creats a node with no child nodes /// /// Value of the node public Node(T value) { Value = value; ChildNodes = new List<Node>(); } /// /// Creates a node […]

在递归方法中使用async / await的正确方法是什么?

在递归方法中使用async / await的正确方法是什么? 这是我的方法: public string ProcessStream(string streamPosition) { var stream = GetStream(streamPosition); if (stream.Items.count == 0) return stream.NextPosition; foreach(var item in stream.Items) { ProcessItem(item); } return ProcessStream(stream.NextPosition) } 以下是使用async / await的方法: public async Task ProcessStream(stringstreamPosition) { var stream = GetStream(streamPosition); if (stream.Items.count == 0) return stream.NextPosition; foreach(var item in stream.Items) { await ProcessItem(item); //ProcessItem() […]