【问题标题】:How to break out of a while loop with a boolean?如何用布尔值打破while循环?
【发布时间】:2013-03-02 02:46:17
【问题描述】:

我正在尝试跳出几个嵌套的 while 循环,但遇到了麻烦。我希望这个程序进入外循环,它只会运行一定的次数。我尝试用布尔值来做,但我的程序终止得太早了。这是一个 N-Queens 问题,我正在求解 1x1、2x2、3x3、...nxn 个皇后。

这是我的代码:

bool ok(int *q, int col)
{
   for(int i=0; i<col; i++)
      if(q[col]==q[i] || (col-i)==abs(q[col]-q[i])) return false;

return true;
};

void print(int q[], int n, int cnt)
{
    //static int count =0;
    cout<<"There are "<<cnt<<" solutions for "<<n<<" queens." <<endl;   
};

int main()
{
    int n;
    int *q;
    cout<<"Please enter the size of the board:"<<endl;
    cin>>n;

    int static count = 0;

    int c = 1;
    int a = 1;
    bool from_backtrack=false;

    while(a!=n){

        q= new int[a];
        q[0]=0;
        bool foundSolution=true;
        while(foundSolution)
        {
            if (c==a){
                a++;
            }
            while(c<a)
            {
                if(!from_backtrack)
                    q[c] = -1; //Start at the top
                from_backtrack=false;
                while(q[c]<a)
                {
                    q[c]++;
                    if  (q[c]==a)
                    {
                        c--;
                        if(c==-1) {
                            print(q, n, count);
                            foundSolution=false;
                            //system("PAUSE"); exit(1);
                        }
                        continue;
                    }
                    if( ok(q,c) ) break; //get out of the closest while loop
                }
                c++;
            }
            count++;
            c--;
            if(c==-1) {
                print(q, n, count);
                foundSolution=false;
                //system("PAUSE"); exit(1);
            }
            from_backtrack=true;
        }
        delete[a] q;
        a++;
    }
    system("PAUSE");
}

【问题讨论】:

  • 你还没有告诉我们“太早”是什么意思。我将重点介绍您为 break 语句编写的条件表达式。
  • while(foundSolution = true) { --可能的错误
  • 它可以使用 goto 完成,但这是不受欢迎的。您很可能会重构您的代码,这样您就不必突破多个级别。
  • 提取一些函数
  • 个人在四个缩进层开始质疑我的逻辑和代码结构。

标签: c++ while-loop nested boolean n-queens


【解决方案1】:

最优雅的方法是将一些内部循环包装在一个函数中。 它会更容易阅读和控制。

【讨论】:

    【解决方案2】:

    在我的工作中,我们采用 MISRA 指南,其中指出“......每个 while 循环只有 1 个中断”。这导致我重写了我的 ifwhile 循环:

    bool can_continue = true;
    
    if (can_continue)
    {
      status = Do_Something();
      if (status != SUCCESS)
      {
        can_continue = false;
      }
    }
    
    if (can_continue)
    {
      status = Do_Another_Thing();
      can_continue = status == SUCCESS;
    }
    
    //.. and so on.
    

    这个想法是如果执行无法继续,则将标志设置为“false”。在任何段可能导致执行失败后检查它。

    【讨论】:

      【解决方案3】:
      while( true ){
      
          if( condition == true ){
              goto bye;
          }
      }
      
      :bye
      

      只是不要在你的作业中提交这个......

      【讨论】:

      • “只是不要在你的家庭作业中提交这个”——在这种情况下,你推荐什么样的软件使用这种方法?高可用性网站后端?金融交易?航空管制?
      • I.e.:如果您认为它对家庭作业来说还不够好,为什么您会推荐使用它?
      • 我试了一下,看看它是否可行。我收到 HEAP CORRUPTION DETECTED 错误。
      • @Chase 永远不要使用goto,即使是开玩笑也不行。 Bad things will happen..
      • “再见;”可以替换为break
      【解决方案4】:

      认为它是疯狂的无用。

      但是,假设您需要 3 次迭代,您将定义一个由 3 个元素组成的 bool 数组(全部设置为 true)。在每次迭代中,将当前元素设置为 false,直到到达数组的末尾。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-01
        • 2017-11-20
        • 1970-01-01
        • 2021-11-09
        • 2020-09-18
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多