June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,35 +1,32 @@
|
|||
function romanencode(n::Integer)
|
||||
const DR = [["I", "X", "C", "M"] ["V", "L", "D", "MMM"]]
|
||||
if n < 1 || n > 4999 throw(DomainError()) end
|
||||
|
||||
DR = [["I", "X", "C", "M"] ["V", "L", "D", "MMM"]]
|
||||
rnum = ""
|
||||
if n > 4999 || n < 1
|
||||
throw(DomainError())
|
||||
end
|
||||
for (omag, d) in enumerate(digits(n))
|
||||
if d == 0
|
||||
omr = ""
|
||||
elseif d < 4
|
||||
omr = DR[omag, 1]^d
|
||||
elseif d < 4
|
||||
omr = DR[omag, 1] ^ d
|
||||
elseif d == 4
|
||||
omr = DR[omag, 1]*DR[omag, 2]
|
||||
omr = DR[omag, 1] * DR[omag, 2]
|
||||
elseif d == 5
|
||||
omr = DR[omag, 2]
|
||||
elseif d < 9
|
||||
omr = DR[omag, 2]*(DR[omag, 1]^(d - 5))
|
||||
elseif d < 9
|
||||
omr = DR[omag, 2] * DR[omag, 1] ^ (d - 5)
|
||||
else
|
||||
omr = DR[omag, 1]*DR[(omag +1), 1]
|
||||
omr = DR[omag, 1] * DR[omag + 1, 1]
|
||||
end
|
||||
rnum = omr*rnum
|
||||
rnum = omr * rnum
|
||||
end
|
||||
return rnum
|
||||
end
|
||||
|
||||
testcases = Int64[1990, 2008, 1668]
|
||||
for i in 1:12
|
||||
push!(testcases, rand(1:4999))
|
||||
end
|
||||
testcases = [1990, 2008, 1668]
|
||||
append!(testcases, rand(1:4999, 12))
|
||||
testcases = unique(testcases)
|
||||
|
||||
println("Test romanencode, arabic => roman:")
|
||||
for i in testcases
|
||||
println(i, " => ", romanencode(i))
|
||||
for n in testcases
|
||||
@printf("%-4i => %s\n", n, romanencode(n))
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue