This commit is contained in:
VicRen
2020-12-02 08:51:48 +08:00
parent c93930c737
commit 410825768e
2 changed files with 79 additions and 0 deletions

32
20201202/main.go Normal file
View File

@@ -0,0 +1,32 @@
package main
var r2d = map[uint8]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
func main() {
}
func RomaToDigit(s string) int {
l := len(s)
ret := 0
for i := 0; i < l; i++ {
c := r2d[s[i]]
ret += c
if i >= l-1 {
break
}
cn := r2d[s[i+1]]
if cn > c {
ret -= 2 * c
}
}
return ret
}

47
20201202/main_test.go Normal file
View File

@@ -0,0 +1,47 @@
package main
import "testing"
func TestRomaToDigit(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want int
}{
{
"test1",
args{"III"},
3,
},
{
"test2",
args{"IV"},
4,
},
{
"test3",
args{"VI"},
6,
},
{
"test4",
args{"LVIII"},
58,
},
{
"test5",
args{"MCMXCIV"},
1994,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RomaToDigit(tt.args.s); got != tt.want {
t.Errorf("RomaToDigit() = %v, want %v", got, tt.want)
}
})
}
}