【问题标题】:What is Innermost loop in imperfectly nested loops?不完美嵌套循环中的最内层循环是什么?
【发布时间】:2011-07-18 09:32:11
【问题描述】:

Helo,在不完美嵌套循环的情况下,我对内部循环的定义有点困惑。考虑这段代码

for (i = 0; i < n; ++i)
{
   for (j = 0; j <= i - 1; ++j)
        /*some statement*/
    p[i] = 1.0 / sqrt (x);
   for (j = i + 1; j < n; ++j)
   {
       x = a[i][j];
       for (k = 0; k <= i - 1; ++k)
          /*some statement*/
       a[j][i] = x * p[i];
   }
}

在这里,我们在同一嵌套级别中有两个循环。但是,在从 j+1 开始迭代“j”的第二个循环中,又存在另一个嵌套级别。考虑整个循环结构,代码中最内层的循环是什么?

【问题讨论】:

  • 这是可能的家庭作业吗?我很好奇你为什么这么问。
  • 听起来像是一个家庭作业问题?为什么你需要知道哪个是最内层的循环?我认为您已经通过参考嵌套级别回答了自己的问题。
  • 我被困在我的论文中,我需要对一些循环进行建模......而一直以来我都看到了完美的嵌套循环......

标签: c loops for-loop nested


【解决方案1】:

两个j 循环都嵌套在i 中,k 是最里面的循环

【讨论】:

    【解决方案2】:

    大声笑我不知道如何解释,所以我会尽力而为我推荐使用debugger!它可能对你有很大的帮助,你甚至不会知道

    for (i = 0; i < n; ++i)
    {
       //Goes in here first.. i = 0..
       for (j = 0; j <= i - 1; ++j) {
            //Goes here second..
            //Goes inside here and gets stuck until j is greater then (i- 1) (right now i = 0)
            //So (i-1) = -1 so it does this only once.
            /*some statement*/
        p[i] = 1.0 / sqrt (x);
       }
       for (j = i + 1; j < n; ++j)
       {
          //Goes sixth here.. etc.. .. 
          //when this is done.. goes to loop for (i = 0; i < n; ++i)
    
          //Goes here third and gets stuck
          //j = i which is 0 + 1.. so, j == 1
          //keeps looping inside this loop until j is greater then n.. idk what is n..
          //Can stay here until it hits n.. which could be a while.
           x = a[i][j];
           for (k = 0; k <= i - 1; ++k) {
               //Goes in here fourth until k > (i-1).. i is still 0..
               //So (i-1) = -1 so it does this only once
              /*some statement*/
           a[j][i] = x * p[i];
          }
          //Goes here fifth.. which goes.... to this same loop!
       }
    }
    

    【讨论】:

      【解决方案3】:

      我会说k 是最里面的循环,因为如果你计算从外部到达它所需的循环数,它是三个循环,这是所有四个循环中最多的循环你的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多