【问题标题】:C++ infinite loop issueC++无限循环问题
【发布时间】:2016-04-17 06:24:05
【问题描述】:

我不确定问题可能是什么。我添加了“cout

但现在我看到发生了一个无限循环。

我实际上是在尝试计算 num = 0 需要多少次循环。

起初我以为是 num = count 在循环中。

有什么建议吗?提前致谢。

#include <iostream>
using namespace std;

// hailstone function prototype
int hailstone(int &num);


int main()
{
    int val;

    cout << "Enter integer" << endl;
    cin >> val;

    hailstone(val);

    cout << val << endl;

    return 0;


}

// Pass values by reference to hailstone function header
int hailstone(int &num)
{   

    do
    {
        int count = 0;    

        // If num is even
        // Divide by two
        if(num % 2 == 0)
        {
            num = (num / 2);
            count++;
            cout << count;

        }

        // If num is odd
        // Multiply by 3 and add 1
        else if(num % 2 != 0)
        {
            num = (num * 3) + 1;
            count++;
            cout << count;

        }
    // Assign the number of steps to num
    num = count;

    } while(num > 0);

    // Return the number of steps
    return num;
}

【问题讨论】:

  • 你到底想做什么? count 总是大于 0 并且在循环结束时将它分配给 num 以便循环永远继续
  • 在为您的算法编写代码之前,您的算法是否有意义?一眼看去,即使没有声明 num = count;,我也看不出 num 将如何长期递减 - 你会有一个无限循环 1 -> 4 -> 2 -> 1 -> 4 -> 2 -> 1 -> ...回应@Pooya,你想做什么?
  • 只需删除“num = count;”并将“return num”替换为“return count”,你就很成功了——至少和你的偶/奇算法一样好——但也许你真的在寻找 num 去 1,而不是 0。跨度>

标签: c++ infinite-loop


【解决方案1】:

这里有一些问题,但最相关的是这一行:

// Assign the number of steps to num
num = count;

根据冰雹序列的规则更新 num 后,您实际上是在重置 num。因为 'count' 在每个循环开始时被初始化,然后在 'if' 或 'else if' 块中迭代一次,所以它总是一个。因此,在每个循环结束时,num 始终为 1。

【讨论】:

  • "num 始终为 1"?
  • @xaxxon 阅读了他的代码。在每个循环期间,count 被初始化为零、递增并分配给 num。因此,num 始终等于 1。
  • 是的,我错过了循环内的初始化。,
【解决方案2】:

无论你是在做num = num/2;(偶数)还是num = (num * 3) + 1(奇数),数字都不会变成0 or less。这就是您的代码运行无限时间的原因。因此,您需要将函数hailstone 内的while (num &gt; 0); 行更改为while(num &gt; 1);(可能是第54 行)。如下:

旧代码:

int hailstone(int &num)
{   

do
{
    int count = 0;    

    // If num is even
    // Divide by two
    if(num % 2 == 0)
    {
        num = (num / 2);
        count++;
        cout << count;

    }

    // If num is odd
    // Multiply by 3 and add 1
    else if(num % 2 != 0)
    {
        num = (num * 3) + 1;
        count++;
        cout << count;

    }
// Assign the number of steps to num
num = count;

} while(num > 0); //You need to change this line

// Return the number of steps
return num;
}

新代码:

int hailstone(int &num)
{   

do
{
    int count = 0;    

    // If num is even
    // Divide by two
    if(num % 2 == 0)
    {
        num = (num / 2);
        count++;
        cout << count;

    }

    // If num is odd
    // Multiply by 3 and add 1
    else if(num % 2 != 0)
    {
        num = (num * 3) + 1;
        count++;
        cout << count;

    }
// Assign the number of steps to num
num = count;

} while(num > 1); //Here is the change

// Return the number of steps
return num;
}

另一个问题:您声明了num = count(在第 52 行),它不会返回实际值(如果您的代码用于计算循环数)。示例:如果输入为 12,则循环将运行 10 次,但您的代码将打印 11,我不认为这一行是必须的。在 do-while 循环中断后返回 count 变量的值。 谢谢

【讨论】:

    【解决方案3】:

    显然,'if' 或 'else if' 条件将被满足,这将使 count 增加到 1 ,并且分配 num =count 使得 num =1 ,so(num>0) 始终为真。所以循环永远继续

    【讨论】:

      【解决方案4】:
      做 { 整数计数 = 0; 如果(数字 % 2 == 0) { 数 = (数 / 2); count++; cout count++; cout num = 计数; } while (num > 0);

      【讨论】:

      • 是的,我做到了。这应该足够了。
      • 哦,你把错误加粗了?这在我的字体中相当微妙,并且比简单地解释问题需要更多的工作。在我看来,很俗气
      猜你喜欢
      • 2015-04-29
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2021-09-02
      相关资源
      最近更新 更多