為什么這個 R 代碼回傳 1 1 0?我明白為什么最后有一個零,因為 6 除以 2 的余數為 0。但我不確定我們是如何得到 1 1 的。我認為 6 除以 2 是 3。我的意思是,好吧,我猜測 Recall() 意味著該函式將被重復,所以我們得到 3 除以 2 是 1。但無論如何,我不明白為什么有兩個 1。有人可以解釋一下嗎?謝謝。
binary <- function(x) {
if(x == 0)
numeric()
else
c(Recall(x %/% 2), x %% 2)
}
binary(6)
uj5u.com熱心網友回復:
我們可以運行相同的函式,但使用一些cat陳述句,以便遞回函式可以解釋它在做什么,以及以什么順序。請注意,除了cat呼叫之外,功能完全相同。
binary <- function(x) {
# Statement 1
cat("Function was given the value", x, "\n")
if(x == 0){
# Statement 2
cat(" - Value of 0 passed, therefore stopping.\n\n")
numeric()
}
else
{
# Statement 3
cat(" - inserting the remainder (", x%%2, ") to the result\n")
c(Recall(x %/% 2), x %% 2)
}
}
所以現在我們可以看到:
binary(6)
#> Function was given the value 6
#> - inserting the remainder ( 0 ) to the result
#> Function was given the value 3
#> - inserting the remainder ( 1 ) to the result
#> Function was given the value 1
#> - inserting the remainder ( 1 ) to the result
#> Function was given the value 0
#> - Value of 0 passed, therefore stopping.
#>
#>[1] 1 1 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318533.html
標籤:r
