简体   繁体   English

如何避免此程序中的无限循环

[英]How to avoid infinite loop in this program

I am trying to solve a 3n+1 problem in C++. 我正在尝试解决C ++中的3n + 1问题。 I want to take the input in pairs and calculate its maximum cycle length then output it. 我想成对输入并计算其最大循环长度,然后输出。

I/P: 1 10
     100 200
     201 210
     900 1000
O/P: 1 10 25
     100 200 125
     201 210 89
     900 1000 174

Code: 码:

#include<iostream>
using namespace std;

int clen(int i)
{
    int len=1;
    while(i!=1)
    {
        len++;
        if(i%2==0)
            i/=2;
        else
            i=3*i+1;
    }
    return len;
}

int maxclen(int a, int b)
{
    int i,len,maxlen=0;
    for(i=a;i<=b;i++)
    {
        len=clen(i);
        if(len>maxlen)
            maxlen=len;
    }
    return maxlen;
}

main()
{
    int b[10][2],l,i,len[10];
    for(i=0;;i++)
    {
        cin>>b[i][0];
        if(cin.fail())
        {
            cin.clear();
            goto a;
        }
        cin>>b[i][1];
        if(cin.fail())
            goto a;

    }
    a:
    l=i;
    for(i=0;i<=l;i++)
    {
        if(b[i][1]>b[i][0])
            len[i]=maxclen(b[i][0],b[i][1]);
        else
            len[i]=0;
    }
    for(i=0;i<l;i++)
        cout<<b[i][0]<<" "<<b[i][1]<<" "<<len[i]<<endl;
}

I want to stop entering the input whenever cin.fail() returns true but its working for few first execution of the program but after that it is entering in an infinite loop to enter the numbers and it just cant get out. 每当cin.fail()返回true时,我都想停止输入输入,但是它对于程序的首次执行几乎没有作用,但是此后它进入无限循环以输入数字,并且无法退出。 Can anyone help me on this cin issue, How can I avoid this infinite loop? 谁能帮助我解决这个问题,如何避免这种无限循环?

I found it died if you hit ctrl+d (eof) after only entering one number. 我发现如果仅输入一个数字后按ctrl + d(eof),它就会死亡。

Maybe try adding another 'clear' the error state there: 也许尝试在此处添加另一个“清除”错误状态:

main()
{
    int b[10][2],l,i,len[10];
    for(i=0;;i++)
    {
        cin>>b[i][0];
        if(cin.fail())
        {
            cin.clear();
            goto a;
        }
        cin>>b[i][1];
        if(cin.fail())
        {
            cin.clear(); // ADDED LINE
            goto a;
        }
    }
 ...

Though I wasn't able to reproduce the error 100% .. that seemed to help the behaviour with me. 虽然我无法100%重现错误,但似乎可以帮助我解决问题。

Also your array is only 10 long .. maybe it's reading too much and going into some weird state ? 而且你的数组只有10长..也许它读得太多,并进入某种奇怪的状态?

You have an off-by-one error over here: 您在这里遇到一个错误:

for(i=0;i<=l;i++)
{
  // ...
}

I modified your main like this to correct a couple of problems: 我像这样修改了您的主体,以解决几个问题:

int main()
{
    const size_t length = 10;
    int b[length][2], len[length];

    size_t i = 0;
    while(i < length)
    {
        cin >> b[i][0];
        if(!cin) break;
        cin >> b[i][1];
        if(!cin) break;
        ++i;
    }

    for(int j = 0; j < i; j++)
    {
        if(b[j][1] > b[j][0])
            len[j] = maxclen(b[j][0], b[j][1]);
        else
            len[j] = 0;
    }
    for(int j = 0; j < i; j++)
        cout << b[j][0] << " " << b[j][1] << " " << len[j] << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM