好的编程难题需要大量条件逻辑吗?
什么是需要大量条件逻辑和分支才能解决的经典编程难题?
谢谢
What is a classic programming puzzle which will require a lot of conditional logic and branches to solve?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
《八皇后》和《河内塔》都是经典。解决数独也很有趣,实际上是一个变相的图形着色问题。
Eight queens and Towers of Hanoi are classics. Solving Sudoku is quite interesting too and is really a graph coloring problem in disguise.
我喜欢编写一个计算 x^N 的最少乘法次数的函数(例如,对于 N = 30,您可以用 6 次乘法来计算 x^N - x -> x^2 -> x^ 3 -> x^5 -> x^15 -> x^30
没有已知的有效算法,因此必须使用条件逻辑和分支。当您尝试绑定时,请参阅此维基百科文章了解更多详细信息 - http://en.wikipedia.org/维基/Addition_chain
I like the problem of writing a function that computes the least number of multiplications to compute x^N (e.g., for N = 30, you can compute x^N with 6 multiplies - x -> x^2 - > x^3 -> x^5 -> x^10 -> x^15 -> x^30.
There's no known efficient algorithm, so you have to use branch-and-bound. The conditional logic and branching come in when you are trying to bound. See this wikipedia article for more details - http://en.wikipedia.org/wiki/Addition_chain
数独
Sudoku
这里有很多小的编程难题。很多都是关于逻辑的
Here are a lot of small programming puzzles. a Lot of them are about logic
如果目的是进行面试(即您需要让候选人在评估过程中敲出一些代码),我一直喜欢 欧拉项目问题 11。
如果是为了自己学习一门新语言等,我更喜欢 codingdojo.org 上的一些代码 kata ( OCR 在任何语言中都非常有趣)。
If the purpose is for an interview (i.e. you need to have a candidate bang out some code as part of the evaluation process), I've always liked Project Euler question 11.
If it's for your own use to learn a new language, etc. I prefer some of the code kata at codingdojo.org (the OCR one is pretty entertaining in any language).