这是递归跟踪的解释,我无法理解其中的一些逻辑。请帮助我的逻辑
This is explanation on recursion tracing and i can't understand some logic in it. Please help me with the logic
图中写的函数是为了解释递归。它说该函数需要 T(n) 时间到 运行 但它包含一个递归调用然后它需要 T(n-1) 时间到 运行。但是我们知道函数需要 T(n) 时间并且进行相同的函数调用然后它应该再次花费 T(n) 时间因为函数正在做与以前相同的事情。如果我对概念的理解有误或代码中对概念的解释有误,请告诉我。
图片是这样的
void Test(int n) {
if (n > 0) {
printf("%d",n); // 1
Test(n-1); // T(n-1)
}
}
// T(n) = T(n-1) + 1
根据定义 T(n)
是 Test(n)
所用的时间。另外 T(n-1)
只是“调用 Test(n-1)
所用时间”的标签。
由于函数只调用了printf
,复杂度不变,算作1条指令,调用了Test(n-1)
,总运行时间为T(n) = T(n-1) + 1
.
But we know that function takes T(n) time and same function call is taken then it should again take T(n) time because function is doing same thing as before.
不,T(n-1)
与 T(n)
不同,因为 Test(n)
与 Test(n-1)
不同。 Test(n)
的作用与 Test(n-1)
相同,只是调用 printf
一次。
图中写的函数是为了解释递归。它说该函数需要 T(n) 时间到 运行 但它包含一个递归调用然后它需要 T(n-1) 时间到 运行。但是我们知道函数需要 T(n) 时间并且进行相同的函数调用然后它应该再次花费 T(n) 时间因为函数正在做与以前相同的事情。如果我对概念的理解有误或代码中对概念的解释有误,请告诉我。
图片是这样的
void Test(int n) {
if (n > 0) {
printf("%d",n); // 1
Test(n-1); // T(n-1)
}
}
// T(n) = T(n-1) + 1
根据定义 T(n)
是 Test(n)
所用的时间。另外 T(n-1)
只是“调用 Test(n-1)
所用时间”的标签。
由于函数只调用了printf
,复杂度不变,算作1条指令,调用了Test(n-1)
,总运行时间为T(n) = T(n-1) + 1
.
But we know that function takes T(n) time and same function call is taken then it should again take T(n) time because function is doing same thing as before.
不,T(n-1)
与 T(n)
不同,因为 Test(n)
与 Test(n-1)
不同。 Test(n)
的作用与 Test(n-1)
相同,只是调用 printf
一次。