无限循环:确定和突破无限循环 [英] Infinite Loop : Determining and breaking out of Infinite loop

查看:146
本文介绍了无限循环:确定和突破无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何确定一个循环是一个无限循环,并会打破它。

How would you determine a loop is a infinite loop and will break out of it.

有人有算法或可以帮助我解决这个问题吗?

Does anyone has the algorithm or can assist me on this one.

谢谢

推荐答案

没有通用的算法可以确定对于每种图灵完整语言,程序是否处于无限循环状态,这基本上是停止问题

There is no general case algorithm that can determine if a program is in an infinite loop or not for every turing complete language, this is basically the Halting Problem.

证明它的想法是简单:


  1. 假设您有这样的算法 A

  2. 构建一个程序 B 对其自身调用 A [在 B上]。

  3. 如果 A 回答程序将停止-进行无限循环

  4. 其他[ A 答案 B 不会停止]-立即停止

  1. Assume you had such an algorithm A.
  2. Build a program B that invokes A on itself [on B].
  3. if A answers "the program will halt" - do an infinite loop
  4. else [A answers B doesn't halt] - halt immidiately

现在,阿苏我在 B 上调用 A -答案肯定是错误的,因此是 A 不存在。

Now, assume you invoke A on B - the answer will be definetly wrong, thus A doesn't exist.

注意:以上内容不是形式上的证明,只是它的草图。

Note: the above is NOT a formal proof, just a sketch of it.

这篇关于无限循环:确定和突破无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆