【问题标题】:For loop not working? [closed]For循环不起作用? [关闭]
【发布时间】:2012-05-17 14:01:30
【问题描述】:

我为我的 C++ 类编写了这个函数,我尝试了 x = 4,但是当我跟踪它时,我没有看到它像应有的那样循环。它所做的是获取一个包含 5 个数字的数组并对它们进行排序,然后检查该组合是否构成满堂彩。

bool isFullHouse(int)
    {
        int match = 0;
        BubbleSort(DiceHeld, 5);
        for ( int x=0; x <= 4; x++ )
        {
            if (((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+2) && (DiceHeld[4] == x+2) && (DiceHeld[5] == x+2)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+1) && (DiceHeld[4] == x+2) && (DiceHeld[5] == x+2)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+3) && (DiceHeld[4] == x+3) && (DiceHeld[5] == x+3)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+1) && (DiceHeld[4] == x+3) && (DiceHeld[5] == x+3)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+4) && (DiceHeld[4] == x+4) && (DiceHeld[5] == x+4)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+1) && (DiceHeld[4] == x+4) && (DiceHeld[5] == x+4)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+5) && (DiceHeld[4] == x+5) && (DiceHeld[5] == x+5)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+6) && (DiceHeld[4] == x+2) && (DiceHeld[5] == x+2)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+6) && (DiceHeld[4] == x+6) && (DiceHeld[5] == x+6)) ||
                ((DiceHeld[1] == x+1) && (DiceHeld[2] == x+1) && (DiceHeld[3] == x+1) && (DiceHeld[4] == x+6) && (DiceHeld[5] == x+6)))
            {
                match = 1;
            }
        }
        if (match == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

【问题讨论】:

  • 它没有像它应该的那样循环?它的循环与您的预期有何不同?
  • 我不能评论那个循环的 contents -- oy -- 但循环本身看起来不错,应该迭代五次。
  • 您确定索引正确吗?您说您有 5 个数字的数组,但您使用的是 DiceHeld[5],它是数组的第六个元素。
  • 您是否正在编译优化?我可以很容易地看到 match == 1 测试被优化为 break 以便它提前停止......
  • 计算每个数字出现的次数要简单得多,然后您可以检查某个数字出现了两次,而另一些数字出现了 3 次。等效地,您可以检查每个数字出现 0、2 或 3 次,或者没有数字出现 1、4 或 5 次。

标签: c++ arrays for-loop


【解决方案1】:

为了好玩,这里有一个不需要如此繁重的逻辑或排序的版本:



static const int kNumDice = 5;

bool isFullHouse()
{
    // There are only 2 possible patterns for full house on a set of numbers, sorted or not
    // Either the first 3 dice have the same value and the 4th and 5th dice are the same
    // or the first 2 dice match and the 3rd, 4th, 5th dice match 

    int currentMatch = DiceHeld[0];
    int uniqueValues = 1;
    int matchLength = 1;

    for(int i=1; i&ltkNumDice; i++)
    {
        // Start next match
        if(DiceHeld[i] != currentMatch)
        {
            if(matchLength &lt 2)
            {
                return false; 
            }

            if(++uniqueValues > 2)
            {
                return false;
            }

            currentMatch = DiceHeld[i];
            matchLength = 1;
        }
        else if(++matchLength > 3)
        {
            return false;
        }
    }

    return true;
}

【讨论】:

    【解决方案2】:

    DiceHeld 的索引可能应该在 0 到 4 之间。根据您的编译器和调试器,内存错误可能不明显,但仍会发生。

    我还打赌,退出循环后match 不是 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-08
      • 2020-08-03
      • 2022-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多