C++ 我的循环语句有什么问题?

C++ 我的循环语句有什么问题?,c++,loops,for-loop,while-loop,scope,C++,Loops,For Loop,While Loop,Scope,我试图做一个数独棋盘检查功能,看看输入的棋盘是否有效。要做到这一点,我将生成一块板,并检查每一行、每一列和9个正方形的集合,看看它们是否都包含数字1-9。如果它们不包含该特定区域中的每个数字,则函数将 返回false 现在,我的重点是检查数独板上的行。我不想在检查编译版本时输入所有9行,所以我现在只关注一行 程序检查行中是否包含所有9个数字。如果该行缺少一个数字(1-9),它将返回false到功能并显示“无效板!” 然而,程序总是说它是一个有效的电路板,即使它缺少一些必需的数字 以下是我迄今为止

我试图做一个数独棋盘检查功能,看看输入的棋盘是否有效。要做到这一点,我将生成一块板,并检查每一行、每一列和9个正方形的集合,看看它们是否都包含数字1-9。如果它们不包含该特定区域中的每个数字,则函数将
返回false

现在,我的重点是检查数独板上的行。我不想在检查编译版本时输入所有9行,所以我现在只关注一行

程序检查行中是否包含所有9个数字。如果该行缺少一个数字(1-9),它将
返回false到功能并显示“无效板!”

然而,程序总是说它是一个有效的电路板,即使它缺少一些必需的数字

以下是我迄今为止的代码副本:

#include <iostream>
using namespace std;

const int DIMEN = 9;

bool is_valid(int []);

int main()
{
    int board[DIMEN];
    cout << "Enter values for array.\n";
    for (int arraynumber = 0; arraynumber < DIMEN; arraynumber++)
    {
        cout << "\nArray [" << arraynumber << "]?   ";
        cin >> board[arraynumber];
    }   
    bool valid = is_valid(board);
    if (valid)
    {
        cout << "\nValid Board!\n";
    }
    else 
    {
        cout << "\nInvalid Board!\n";
    }
    return 0;
}

bool is_valid(int isvalid[])
{
    bool check_row = false; 
    //Checks to see if the row has all required numbers
    bool check_number = false;
     //Checks to see if the row contains a specific number in it

        while (!(check_row))
        //While the row doesn't yet have all required numbers in it
        {   
            for (int number = 1; number <= DIMEN; number++)
            // Goes through each # (1-9) to see if the row contains that #
            {
                while (!(check_number))
                //While the row doesn't yet have the number in it
                {
                    for (int i = 0; i < DIMEN; i++)
                    //Move across the row from column 0 to 8
                    {
                        if (isvalid[i] == number)
                        /* If the value for this specific element of the array
                        equals the number */
                        {
                            check_number = true;
                            //The row has the number in it
                        }
                    }
                    if (!(check_number))
                    /* If the number was not found in the row by the 
                    end of the for loop */
                    {   return false;
                        //End the program saying the board is invalid
                    }
                }
            }
            check_row = true;
        }
    return true;
}
#包括
使用名称空间std;
常数int DIMEN=9;
bool是有效的(int[]);
int main()
{
集成电路板[DIMEN];

当你开始检查一个新号码时,不能将“检查号码”设置回false

话虽如此,您的代码非常复杂且难以阅读。您可以:

bool is_valid(int row[])
{
    for(int number = 1; number <= 9; number++)
    {
        bool found = false;
        for(int i = 0; i < DIMEN; i++) {
            found = (row[i] == number);
            if (found) break;
        }
        // We've looked at each spot in the row and not found number
        // so we know the row isn't valid.
        if (!found) return false;
    }
    // If we get here we must have found every number so the row is valid
    return true;
}
bool是有效的(int行[])
{

对于(int number=1;number您的函数似乎不必要地复杂。我不清楚它应该如何工作。这里有一个简化的版本应该可以工作

bool is_valid(int board[])
{
   // start with board_state = {0, 0, 0, 0, 0, 0, 0, 0, 0}
   // if the board is valid, we will end up with 
   // board_state = {1, 1, 1, 1, 1, 1, 1, 1, 1}
   int board_state[DIMEN] = {0};
   for ( int i = 0; i < DIMEN; ++i )
   {
      int n = board[i];
      ++board_state[n-1];
   }

   for ( int i = 0; i < DIMEN; ++i )
   {
      if ( board_state[i] != 1 )
      {
         return false;
      }
   }

   return true;
}
bool是有效的(int板[])
{
//从board_state={0,0,0,0,0,0,0,0}开始
//如果董事会有效,我们将以
//board_state={1,1,1,1,1,1,1,1,1}
int board_state[DIMEN]={0};
对于(int i=0;i
问题在于:

 if (isvalid[i] == number)
                    /* If the value for this specific element of the array
                    equals the number */
                    {
                        check_number = true;
                        //The row has the number in it
                    }
一旦你做到这一点,它将永远是真实的功能

因此,如果您找到一个数字,您将返回true

注意: 我之前玩数独游戏时:

  • 创建了一个短x,即16位,将其设置为0
  • 当我找到一个数字n时,左移1(n-1)
  • 按位或运算:移位数|短x
  • 如果短字符为0b111111111,即511十进制或1FF十六进制,则只有完整的行、列或正方形

这将是一个更简单的实现:循环一次以设置位,然后验证一次。

解决此类问题的正确工具是调试器。在询问堆栈溢出之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]你的问题包括一个重现你的问题的例子,以及你在调试器中所做的观察。当我读到这篇文章时,我想到了一个想法。阅读一篇关于程序圈复杂度的文章。对于一个程序来说,减少圈复杂度是一个很好的习惯。你在一段时间内有一个if in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in a in快点,我看看能不能找到什么