也就是 “交互式的输入/从命令行读取”
最简单的就是用 readline
, 详细的参数见help:?readline
。再加个 println
就能实现简单的交互
julia> println("input number of people: [n,m]")
input number of people: [n,m]
julia> ipt = readline(stdin)
5,6
"5,6"
julia> split(ipt, ',')
2-element Array{SubString{String},1}:
"5"
"6"
或者你想要更稍微复杂一点的类 REPL 形式的交互见:
涉及到具体的问题的就先不说了,我先回答下其他问题。而且以 “傳教士 食人族 渡河” 为关键词再 google 上能搜到繁体中文/简体中文的具体算法分析讲解文章不少。英文关键词应该是 Missionaries and cannibals problem - Wikipedia
就是建立一个数组的数组嘛,Julia 里就是 Array
从 1~n 维都可以,并且可以嵌套。
这里就简单演示下如何构建数组的数组
julia> res = []
0-element Array{Any,1}
julia> for _ in 1:10
m, c, l = rand(1:10), rand(1:10), rand(0:1)
push!(res, [m, c, l])
end
julia> res
10-element Array{Any,1}:
[2, 8, 0]
[3, 7, 1]
[5, 7, 0]
[8, 10, 0]
[10, 9, 1]
[4, 6, 1]
[9, 3, 0]
[7, 2, 0]
[2, 8, 0]
[7, 3, 1]
关于多维数组的操作:
https://docs.juliacn.com/latest/manual/arrays/
你有一个数组的数组,对最外层数组循环就行了。还要从另一个数组中拿东西,就再加一层循环。
julia> boat_case = [(2,0) , (1,1), (0,2)]
3-element Array{Tuple{Int64,Int64},1}:
(2, 0)
(1, 1)
(0, 2)
julia> for (i, j) in boat_case
# do sth
println("(i, j)=", (i, j))
for (m, c, l) in res # 使用的是上一部分中的 res 变量
# do sth
println("(m, c, l)=", (m, c, l))
end
end
(i, j)=(2, 0)
(m, c, l)=(2, 8, 0)
(m, c, l)=(3, 7, 1)
(m, c, l)=(5, 7, 0)
(m, c, l)=(8, 10, 0)
(m, c, l)=(10, 9, 1)
(m, c, l)=(4, 6, 1)
(m, c, l)=(9, 3, 0)
(m, c, l)=(7, 2, 0)
(m, c, l)=(2, 8, 0)
(m, c, l)=(7, 3, 1)
(i, j)=(1, 1)
(m, c, l)=(2, 8, 0)
(m, c, l)=(3, 7, 1)
(m, c, l)=(5, 7, 0)
(m, c, l)=(8, 10, 0)
(m, c, l)=(10, 9, 1)
(m, c, l)=(4, 6, 1)
(m, c, l)=(9, 3, 0)
(m, c, l)=(7, 2, 0)
(m, c, l)=(2, 8, 0)
(m, c, l)=(7, 3, 1)
(i, j)=(0, 2)
(m, c, l)=(2, 8, 0)
(m, c, l)=(3, 7, 1)
(m, c, l)=(5, 7, 0)
(m, c, l)=(8, 10, 0)
(m, c, l)=(10, 9, 1)
(m, c, l)=(4, 6, 1)
(m, c, l)=(9, 3, 0)
(m, c, l)=(7, 2, 0)
(m, c, l)=(2, 8, 0)
(m, c, l)=(7, 3, 1)
参见 “b. ” 的回答,向一个空数组中 push!
东西就行了,没有长度限制,可以不初始化。