我正在尝试确定三重嵌套循环的频率计数。
for i = 1 to n do
for j = 1 to i do
for k = i to j do
x = x + 1
我知道语句x = x + 1
在获得n
有关如何入门的任何提示/建议?
答案 0 :(得分:1)
让我们以4和5为例。当i = 4时,
...
for j = 1 to 4 do
for k = 4 to j do
x = x + 1
...j = 1
for k = 4 to 1 do // 4 times
x = x + 1
...j = 2
for k = 4 to 2 do // 3 times
x = x + 1
...j = 3
for k = 4 to 3 do // twice
x = x + 1
...j = 4
for k = 4 to 4 do // once
x = x + 1
当i = 5时,
...
for j = 1 to 5 do
for k = 5 to j do
x = x + 1
...j = 1
for k = 5 to 1 do // 5 times
x = x + 1
...j = 2
for k = 5 to 2 do // 4 times
x = x + 1
...j = 3
for k = 5 to 3 do // 3 times
x = x + 1
...j = 4
for k = 5 to 4 do // twice
x = x + 1
...j = 5
for k = 5 to 5 do // once
x = x + 1
图案?