告诉转义字符串中十六进制的结尾(Tell where escaped hexadecimal ends in a string [duplicate])
我想用
printf
功能打印10摄氏度通常我会这样做:
printf("10\xF8Celsius");
其中\ xF8是度符号的ANSI代码。 问题是编译器在\ x之后采用所有十六进制字符并尝试转换为字符,基本上它需要
\F8Ce
并尝试将其转换为字符。我可以这样写:
printf("10\xF8 Celsius"); //see additional space
但问题依然存在。
如何告诉编译器我的十六进制代码结束? 可能吗?
注意:我在Windows 8.1上使用Visual Studio 2015 PRE来观察这个问题(不是说这个问题是特定于平台的,而只是提及它)
This question already has an answer here:
I want to print 10°Celsius with
printf
functionNormally I would do it like this:
printf("10\xF8Celsius");
where \xF8 is the ANSI code for degree sign. The problem is that the compiler take all hexadecimal characters after \x and tries to convert to a character, basically it takes
\F8Ce
and tries to convert it to a character.I could write it like:
printf("10\xF8 Celsius"); //see additional space
but the question still remains.
How to tell compiler where my hexadecimal code ends? Is it possible?
Note: I used Visual Studio 2015 PRE on a Windows 8.1 to observe this problem (not that this problem is platform specific but just to mention it)
原文:https://stackoverflow.com/questions/31239524
最满意答案
您正在使用递归进行DFS(深度优先搜索/遍历)。
其深度首先是因为递归的工作原理与堆栈的工作方式相同 - 在处理下一个节点之前处理当前节点的子节点 - 所以先深入而不是宽度。
编辑:
针对您的评论/更新问题:您的代码将逐项按顺序处理,不会有任何并行处理,也不会涉及“魔术”。 使用递归的遍历等价于使用堆栈(LIFO = last in,first out) - 它只是隐含的。 所以你的方法也可以写成如下,它产生相同的遍历顺序:
public void SomeMethod(TreeNode root) { Stack<TreeNode> nodeStack = new Stack<TreeNode>(); nodeStack.Push(root); while (nodeStack.Count > 0) { TreeNode node = nodeStack.Pop(); //do something on item //need to push children in reverse order, so first child is pushed last foreach (TreeNode item in node.Nodes.Reverse()) nodeStack.Push(item); } }
我希望这可以更清楚地说明发生了什么 - 在处理控制台时将节点写出来或者实际上一步一步地通过调试器逐步浏览可能对您有用。
(同样,递归方法和使用堆栈的方法都假设没有循环,也不测试 - 所以假设是树而不是任何图。对于后来的DFS引入
visited
标志来标记节点已经存在看到)You are doing a DFS (Depth first search/traversal) right now using recursion.
Its depth first because recursion works the same way as a stack would - you process the children of the current node before you process the next node - so you go for depth first instead of breadth.
Edit:
In response to your comment / updated question: your code will be processed sequentially item by item, there will be no parallel processing, no "magic" involved. The traversal using recursion is equivalent to using a stack (LIFO = last in, first out) - it is just implicit. So your method could also have been written like the following, which produces the same order of traversal:
public void SomeMethod(TreeNode root) { Stack<TreeNode> nodeStack = new Stack<TreeNode>(); nodeStack.Push(root); while (nodeStack.Count > 0) { TreeNode node = nodeStack.Pop(); //do something on item //need to push children in reverse order, so first child is pushed last foreach (TreeNode item in node.Nodes.Reverse()) nodeStack.Push(item); } }
I hope this makes it clearer what is going on - it might be useful for you to write out the nodes to the console as they are being processed or actually walk through step by step with a debugger.
(Also both the recursive method and the one using a stack assume there is no cycle and don't test for that - so the assumption is this is a tree and not any graph. For the later DFS introduces a
visited
flag to mark nodes already seen)
相关问答
更多-
Web爬网程序的BFS或DFS?(BFS or DFS for a web crawler?)[2022-08-30]
我确实观察到我对这个问题的回答有点迟,但是,这是一个有趣的讨论。 BFS似乎是一个很好的策略,因为它可以帮助* 避免在一定程度上连续请求单个主机*。 取决于您的域名。 您仍然需要处理服务器超时的处理,但DFS肯定会造成一些伤害。 同样, 在DFS中,您可以拥有循环引用 , 在无限循环中运行 ; 除非你做出一些明确的安排。 可以有其他更合适的选择,但在DFS和BFS之间, BFS在我看来是赢。 I do observe that I am a bit late in responding to the que ... -
BFS将根据分支因子使用更多的内存...但是,BFS是一个完整的算法...意味着如果您使用它来搜索最低深度的东西,BFS将为您提供最佳解决方案。 BFS空间复杂度是O(b^d) ...分支因子提高到深度(可以是大量的内存)。 另一方面,DFS对于空间好多了,可能会发现次优解决方案。 意思是,如果您只是从一个顶点到另一个顶点搜索路径,您可以在找到实际的最短路径之前找到次优解(并在那里停止)。 DFS空间复杂度是O(|V|) ...意味着它可以占用的最多的内存是最长的可能路径。 他们有相同的时间复杂性。 在实现 ...
-
DFS : 你正在检查迷宫超出界限的界限。(比如if (x < 8) ,但你的迷宫它被声明为char[][] maze = new char[8][8]; 这会生成java.lang.ArrayIndexOutOfBoundsException ,但由于空catch块,此Exception不会被抛出任何位置: } catch (Exception e) { }; 在dfs方法的最后 拜托,不要做这种事! ( 为什么空捕获阻止了一个坏主意? ) 而且,这会打破你的递归。 BFS :我认为使用辅助的int's ...
-
您正在使用递归进行DFS(深度优先搜索/遍历)。 其深度首先是因为递归的工作原理与堆栈的工作方式相同 - 在处理下一个节点之前处理当前节点的子节点 - 所以先深入而不是宽度。 编辑: 针对您的评论/更新问题:您的代码将逐项按顺序处理,不会有任何并行处理,也不会涉及“魔术”。 使用递归的遍历等价于使用堆栈(LIFO = last in,first out) - 它只是隐含的。 所以你的方法也可以写成如下,它产生相同的遍历顺序: public void SomeMethod(TreeNode root) { ...
-
使用BFS而非DFS在有向图中查找周期的任何优点(any advantages of using BFS over DFS for finding cycles in directed graph)[2022-02-06]
在进行BFS时,您将在仅开发O(B^d)节点时找到循环,其中B是分支因子, d是从源到它的循环+标头的大小。 (如果BFS的来源在循环中,则循环的长度)。 DFS不能保证这一点,并且可能在找到周期之前发现整个图形。 When doing BFS, you will find the cycle while developing only O(B^d) nodes, where B is the branch factor and d is the size of the cycle + header fro ... -
BFS和DFS之间的区别(Difference between BFS and DFS)[2023-02-26]
当它说多个来源时,它指的是搜索的起始节点。 您会注意到算法的参数是BFS(G, s)和DFS(G) 。 这应该已经暗示BFS是单源而DFS不是,因为DFS不会将任何初始节点作为参数。 正如作者所指出的,这两者之间的主要区别在于BFS的结果总是树,而DFS可以是森林(树木的集合)。 这意味着,如果BFS是从节点s运行的,那么它将只构造从s可到达的节点的树,但如果图中有其他节点,则不会触及它们。 但是,DFS将继续搜索整个图,并构建所有这些连接组件的林。 正如他们所解释的那样,这是大多数用例中每种算法的期望结果 ... -
DFS,因为它是深度优先搜索,可能会卡在无限分支中,永远不会到达您正在寻找的顶点。 BFS在每次迭代时都经过与根相同距离的所有顶点,无论它们在哪个分支上,所以它最终会找到所需的顶点。 例: root - > v1 - > v2 - > v3 - > ...永远继续 | - >你。 在此示例中,如果DFS从根开始,然后继续到v1。 它永远不会到达你,因为它进入的分支是无限的。 BFS将从root到v1或u再到另一个。 DFS, since its a Depth first search, could get ...
-
这只有在BFS和DFS使用完全相同的顺序来遍历子项时才可能: 规则1: BFS Traversal : 4 3 5 1 2 8 7 6 | | | | | |-------| | | | DFS Traversal : 4|3 1 7 2 6|5 8 正如这个例子所示,我们可以很容易地知道(3 , 1 , 7 , 2 , 6) 3,1,7,2,6 (3 , 1 , 7 , 2 , 6)属于一个以 ...
-
1)你会用DFS和BFS打什么样的页面? 在大多数情况下,我将使用BFS算法来实现一个蜘蛛,因为我想从网页获得的大多数有价值的信息没有太多的链接深度,否则我认为该网站由于设计糟糕而无法抓取。 如果我想从一个页面获取一些特定数据和来自几个跳跃的其他相关数据,同时我想在蜘蛛运行后很快看到结果,那么我可以选择DFS算法。 说,我想从stackoverflow获取所有标签。 标签页面在这里 。 与此同时,我想让谁回答标签中的问题。 我想检查蜘蛛是否正常运行。 然后我使用DFS算法在蜘蛛运行后很快获得数据标签 - 问 ...
-
Dfs与Bfs混淆(Dfs Vs Bfs confusion)[2021-10-20]
您的困惑可能来自于过多地考虑树木,但BFS和DFS可以在任何图形上运行。 考虑例如具有类似ABCA的循环的图。 如果从A开始先进行广度优先,则首先将B和C添加到列表中。 然后,您将弹出B ,除非它们被标记为已访问,否则您将在列表中添加C和A ,这显然是错误的。 如果您先从A开始深入,那么您将访问B并从那里转到C然后转到A ,除非A已被标记为已访问。 因此,总而言之,无论您采用哪种算法,都需要在第一次看到它时立即标记顶点。 但是,如果您只考虑DAG,您会发现事情变得容易一些,因为您根本没有像上面那样的循环。 ...