Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rd1 = "I(1)V(5)X(10)L(50)C(100)D(500)M(1000)"
Dim sep = {"(", ")"}
Dim rd = rd1.Split(sep, StringSplitOptions.RemoveEmptyEntries)
Dim d1 As New Dictionary(Of String, Integer)
For i = 0 To 12 Step 2
d1.Add(rd(i), CInt(rd(i + 1)))
Next
Dim d2 As New Dictionary(Of String, Integer)
d2.Add("IV", 4)
d2.Add("IX", 9)
d2.Add("XC", 90)
d2.Add("XL", 40)
d2.Add("CD", 400)
d2.Add("CM", 900)
Dim tstr = "IXCDM"
Dim s = 0
While tstr.Length > 0
For Each ds In d2.Keys
If InStr(tstr, ds) > 0 Then
s = s + d2(ds)
tstr = tstr.Substring(2)
End If
Next
For Each ds In d1.Keys
If InStr(tstr, ds) > 0 Then
s = s + d1(ds)
tstr = tstr.Substring(1)
End If
Next
End While
MsgBox(s)
End
End Sub
End Class
沒有留言:
張貼留言