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”的第二个循环中,还有另一个嵌套级别。考虑整个循环结构,这是代码中最内层的循环?
答案 0 :(得分:2)
j
个循环同样嵌套在i
内,k
是最里面的循环
答案 1 :(得分:0)
我会说k
是最里面的循环,因为如果你计算从外部到达它所需的循环数,它就是三个循环,这是所有四个循环中最多的循环在你的代码中循环。
答案 2 :(得分:0)
大声笑我不知道如何解释这个,所以我会尽我所能,我推荐使用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!
}
}