Tag: 递归

C#:遍历对象图时避免无限递归

我有一个对象图,其中每个子对象包含一个引用回其父对象的属性。 是否有任何好的策略忽略父引用以避免无限递归? 我考虑过为这些属性添加特殊的[Parent]属性或使用特殊的命名约定,但也许有更好的方法。

反序列化递归JSON对象

我有一个表示查询表达式的递归JSON对象: { “where”: { “operator”: “AND”, “left”: { “operator”: “=”, “$fieldRef”: “requestor”, “value”: “@me” }, “right”: { “operator”: “=”, “$fieldRef”: “state”, “value”: “Closed” } } } 更多对象也可以出现在JSON层次结构中。 这是一个更复杂的例子: { “where”: { “operator”: “OR”, “left”: { “operator”: “=”, “$fieldRef”: “id”, “value”: “1234” }, “right”: { “operator”: “OR”, “left”: { “operator”: “=”, “$fieldRef”: “orgId”, “value”: “6757” }, […]

使用Task并行计算递归算法

如何使用任务将此顺序递归算法转换为并行递归算法? public static List s = new List(); // random integers, array size is n public static List p = new List(); // random integers, array size is n public static int n = 100; public static int w = 10; static void Main(string[] args) { G1(n, w) } private static int G1(int k, int […]

递归调用buff / unbuff? C#Unity3D

我的目标是进行单次碰撞检测,这将降低与特定持续时间碰撞的物体的移动速度。 到目前为止我尝试了什么: //Class that detects the collision if (other.gameObject.tag == “enemy”) { EnemyMovement enemyMove = other.GetComponent (); if (enemyMove.slowMove != 1) { return; } enemyMove.Slow (2, 0.5f, true); //…. //Class that handles the Enemy-Movement //The slowfactor gets multiplied with the Vector3 that handles the movementspeed of the Character void FixedUpdate () { Movement(); } void […]

C#使用Directory.GetFiles和搜索模式递归目录

我想使用递归在目录结构中找到所有excel文件。 问题是,Directory.GetFiles中使用的搜索模式一次只允许一个扩展。 有没有人知道解决这个问题的方法,还是我必须多次通过目录来寻找特定的扩展? 或者你可以只抓取每个文件,然后遍历该列表寻找特定的扩展名。 无论哪种方式听起来都有点低效。 谢谢

C#纸牌游戏中的最佳卡片选择

问题在于遵循以下规则在游戏的每个时刻选择最佳选项: 您只能选择最左边或最右边的卡。 您的对手将始终先选择,并始终从最左侧或最右侧的牌中选择最高牌。 如果它是一个平局,它会选择最右边的。 考虑到这并不总是最好的选择。 有时赢得胜利是不可能的,但无论如何你必须通过对抗这个对手来展示你可以添加的最高分(或者说策略,让我们说)。 例: Cards: 1 2 4 2 8 4 3 Opponent: 3 4 2 2 = 11 Me: 1 8 4 = 13 在这里,我在第二回合选择了1而不是4,所以我可以选择8。 这就是选择最高卡并不总是最好的原因。 我一直在尝试使用递归来实现这个解决方案,但我不确定它是最好的选择。 有关如何设计此算法的任何想法? [编辑]感谢@PengOne的慷慨帮助。 这是我试图实现的代码,但不幸的是它给了我错误。 我该怎么办呢? 随着我的进步,我正在编辑这个。 static int cardGameValue(List D, int myScore, int opponentScore) { if (D.Count == 0) return myScore; else { if (D[0] […]

递归代替多个嵌套for循环?

我有一些问题尝试更新嵌套的for循环,而不是使用递归。 使用递归时,是否可以从早期的for循环访问a,b和c变量? 下面是我试图转换为递归调用的简单示例。 for(int a= 0; a < 10; a++) { for(int b = 0; b < 20; b++) { for(int c = 0; c < 10; c++) { int[] indexes = new int[3]{a,b,c} collection.add(indexes); } } } 编辑:解决方案需要能够在运行时进行调整,以便用户可以选择需要多少级别。

数组中K个元素的总和等于N.

给定一个数组说nums = {1,2,5,3,6,-1,-2,10,11,12},使用max no of elements(比如maxNums = 3)找到其总和的元素(比如sum = 10)= K. 所以如果要使用maxNums = 3求和= 10则答案是 {1 3 6} {1 -1 10} {1 -2 11} {2 5 3} {2 -2 10} {5 6 -1} {-1 11} {-2 12} {10} 我写了一个递归函数来完成这项工作。 如果没有递归,我该怎么做? 和/或内存较少? class Program { static Int32[] nums = { 1,2,5,3,6,-1,-2,10,11,12}; static Int32 sum = 10; […]

树中带有yield return元素顺序的递归

我有一个递归函数,在给定起始根节点的情况下返回所有子树节点。 private IEnumerable getAllNodesRecursively(Node subnode) { foreach (Node node in subnode.Nodes) getAllNodesRecursively(node); yield return subnode; } 对于以下树结构: A | +–B | +–C | | | +–D | +–E 当我尝试迭代时: foreach (Node n in getAllNodesRecursively(a)) { Console.WriteLine(n); } 该函数返回唯一的A值。 我希望使用yield-return和递归,并检索Preorder中的元素(在本例中为A,B,C,D,E)。 (如果我把收益率的回报放在foreach之前,那么foreach永远不会发生)。 这可能吗?

递归遍历目录树并列出文件名

我正在尝试遍历整个目录树并打印出列表框控件上的所有文件名。 我写了一些代码,但有错误。 不知道我做错了什么。 顺便说一句,这是在Visual Studio中使用WPF的C#中。 以下是Visual Studio中的整个项目解决方案: http : //tinyurl.com/a2r5jv9 如果您不想下载项目解决方案,请参阅MainWindow.xaml.cs中的代码: http : //pastebin.com/cWRTeq3N 我也会在这里粘贴代码。 public partial class MainWindow : Window { private void Button_Click_1(object sender, RoutedEventArgs e) { string sourcePath = @”C:\temp\”; static void DirSearch(string sourcePath) { try { foreach (string d in Directory.GetDirectories(sourcePath)) { foreach (string f in Directory.GetFiles(d)) { listBox1.Items.Add(f); } DirSearch(d); […]