无限循环检测器 [英] Infinite Loop Detector

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

问题描述

如果你不熟悉暂停问题,我不会详细介绍

但是它说你不可能写一个程序

可以判断一个循环是否无限。假设神话无限的所有强大的机器,这是一个谬误建造

。在

现实中我们处理有限的机器,它们能够在一个循环中以两个状态进行,它们要么终止,要么重复自己。在神秘的

停止问题场景中,机器被迫拥有第三个状态

他们从不重复自己......他们仍然教这个问题

对于学校里的孩子来说,作为一种教条,所以当他们看到以下

代码时,往往会出现一些惊喜。在这里,我提交给

你查看一个无限循环的示例,其中包含内置无限循环检测器的
。它可以简单地改进

来运行外部程序,你可以通过指数基础上为buf指定一个新值来加快它的速度。这让我的生活变得非常轻松,现在当我尝试暴力解决时,我知道当我成功或失败时,有时候现在

将可以解决的问题转化为蛮力

解决方案,因为它需要我的工作量更少,但只需要更多

等待。 br />

#include< iostream>

使用命名空间std;

int main(){

int test = 0;

int buf = -1;

while(1){

if(test == buf)

infinite = true;

break;

if(test%2 == 0)

buf ++;

test ++;

if(test%10000000 == 0){

cout<< test<< endl;

cout<< buf<< endl;

}

}

如果(无限==真)

cout<< ;无限循环......<< endl;

其他

cout<<"停止!"<< endl;

返回0;

}

解决方案

嗨Gremlin,

#include< iostream>
使用命名空间std;
int main(){
int test = 0;
int buf = -1;
^ bool无限; while(1){
if(test == buf)
^ {infinite = true;
break;
^} if(test%2 == 0)
buf ++;
test ++;
if(test%10000000 == 0){
cout<< test<< endl;
cout<< buf<< endl;
}
}如果(无限==真)
cout<<""无限循环..."<< endl;
其他
cout<<<"暂停!"<< endl;
返回0;
}




尽管如此,我还是得不到它。对我没有意义。没有。完全没有。


问候,乔


#include< iostream>

using namespace std ;

bool infinite = false;

int main()$

int test = 0;

int buf = -1;

而(1){

if(test == buf){

infinite = true;

休息;

}

if(test%2 == 0)

buf ++;

test ++ ;

if(test%10000000 == 0){

cout<< test<< endl;

cout<< buf<< endl;

}

}

如果(无限==真)

cout<< ;无限循环......<< endl;

其他

cout<<"停止!"<< endl;

返回0;

}


这是因为我的代码中有一些错误。

我忘了宣布bool无限,并且需要在

周围添加一些大括号。


Johannes Bauer< df **** *******@gmx.de>新闻中写道:oe45h1-
vp*****@laptophost.lapt opdomain:
< blockquote class =post_quotes>嗨Gremlin,

#include< iostream>
使用命名空间std;
int main(){
int test = 0;
int buf = -1;


^ bool infinite;

while(1){
if(test = = buf)


^ {

infinite = true;
break;


^}

if (test%2 == 0)
buf ++;
test ++;
if(test%10000000 == 0){
cout<< test<< endl;
cout<< buf<< endl;
}
}如果(无限==真)
cout<<""无限循环..."<< endl;
其他
cout<<<"停止!"<< endl;
返回0;
}



,我不明白。对我没有意义。没有。一点友好。

问候,乔




哎呀,对不起

我忘了几行代码

应该是这样的:


#include< iostream>

使用命名空间std;

bool infinite = false;

int main(){

int test = 0;

int buf = -1 ;

while(1){

if(test == buf){

infinite = true;

休息;

}

if(test%2 == 0)

buf ++;

test ++;

if(test%10000000 == 0){

cout<< test<< endl;

cout<< buf<< endl;

}

}

如果(无限==真)

cout<< ;无限循环......<< endl;

其他

cout<<"停止!"<< endl;

返回0;

}


If you are not familiar with the halting problem, I will not go into
it in detail but it states that it is impossible to write a program
that can tell if a loop is infinite or not. This is a fallacy built
on the assumption of mythical infinite all powerfull machines. In
reality we deal with finite machines that are capable of two states in
a loop, they either terminate, or repeat themselves. In the mythical
halting problem scenario machines are saposed to have a third state
where they never repeat themselves... still they teach this problem
to kids in school as a kind of dogma so when they see the following
code it will often times be some what of a suprise. Here I submit to
you for review an example implimentation of an infinite loop that has
a infinite loop detector built into it. It can deffinitly be improved
to run on external programs and you could speed it up a bit by
asigning a new value to buf on an exponential basis. This has made my
life an awfully lot easyer, now when I try to brute force sollutions I
know when I have succeeded or when I have failed, and sometimes now
turn problems that could be solved otherwise into brute force
solutions because it requires less work on my part but just a bit more
waiting.

#include <iostream>
using namespace std;
int main(){
int test=0;
int buf=-1;
while(1){
if(test==buf)
infinite=true;
break;
if(test%2==0)
buf++;
test++;
if(test%10000000==0){
cout << test<<endl;
cout << buf<<endl;
}
}
if(infinite==true)
cout<<"Infinite Loop..."<<endl;
else
cout<<"Halted!"<<endl;
return 0;
}

解决方案

Hi "Gremlin",

#include <iostream>
using namespace std;
int main(){
int test=0;
int buf=-1; ^ bool infinite; while(1){
if(test==buf) ^ { infinite=true;
break; ^ } if(test%2==0)
buf++;
test++;
if(test%10000000==0){
cout << test<<endl;
cout << buf<<endl;
}
}
if(infinite==true)
cout<<"Infinite Loop..."<<endl;
else
cout<<"Halted!"<<endl;
return 0;
}



Still, I don''t get it. Doesn''t make sense to me. Not any. At all.

Greetings, Joe


#include <iostream>
using namespace std;
bool infinite=false;
int main(){
int test=0;
int buf=-1;
while(1){
if(test==buf){
infinite=true;
break;
}
if(test%2==0)
buf++;
test++;
if(test%10000000==0){
cout << test<<endl;
cout << buf<<endl;
}
}
if(infinite==true)
cout<<"Infinite Loop..."<<endl;
else
cout<<"Halted!"<<endl;
return 0;
}

that''s because my code had a few buggs in it.
I forgot to declare bool infinite, and needed to add some brakets around
one if statement.

Johannes Bauer <df***********@gmx.de> wrote in news:oe45h1-
vp*****@laptophost.laptopdomain:

Hi "Gremlin",

#include <iostream>
using namespace std;
int main(){
int test=0;
int buf=-1;


^ bool infinite;

while(1){
if(test==buf)


^ {

infinite=true;
break;


^ }

if(test%2==0)
buf++;
test++;
if(test%10000000==0){
cout << test<<endl;
cout << buf<<endl;
}
}
if(infinite==true)
cout<<"Infinite Loop..."<<endl;
else
cout<<"Halted!"<<endl;
return 0;
}



Still, I don''t get it. Doesn''t make sense to me. Not any. At all.

Greetings, Joe




whoops, sorry
I forgot a few lines of code
it should look like:

#include <iostream>
using namespace std;
bool infinite=false;
int main(){
int test=0;
int buf=-1;
while(1){
if(test==buf){
infinite=true;
break;
}
if(test%2==0)
buf++;
test++;
if(test%10000000==0){
cout << test<<endl;
cout << buf<<endl;
}
}
if(infinite==true)
cout<<"Infinite Loop..."<<endl;
else
cout<<"Halted!"<<endl;
return 0;
}


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

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