Your task is to return the number of tiny pairs that you'll encounter during the simultaneous iteration through a and b.
equation <- function(a, b, k){
b <- rev(b)
concat <- numeric(length(a))
for (i in seq_along(a)){
concat[i] <- paste0(a[i],b[i])
}
test <- concat < k
result <- sum(test == TRUE)
result
}
b<- c(1, 2, 3)
k<- 31
equation(a, b, k)
运行结果:2 b <- c(7, 11, 2, 0, 15)
k <- 743
equation(a, b, k)
运行结果:5因为concat里面的东西都是字符串,字符串比较和数值比较的结果是不同的
4015<743是FALSE,但"4015"<743是TRUE
所以在比较前先做个格式转换吧:
test <- as.numeric(concat) < k