This commit is contained in:
VicRen
2021-01-14 09:34:07 +08:00
parent dbfd209994
commit e8203bf4fa
2 changed files with 73 additions and 0 deletions

37
20210114/main.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import "fmt"
func main() {
}
func readBinaryWatch(num int) []string {
var ret []string
for h := 0; h <= 12; h++ {
hCount := 0
s := fmt.Sprintf("%#b", h)
for _, c := range s {
if c == '1' {
hCount++
}
}
for m := 0; m <= 60; m++ {
mCount := 0
s := fmt.Sprintf("%#b", m)
for _, c := range s {
if c == '1' {
mCount++
}
}
if hCount+mCount == num {
if m > 10 {
ret = append(ret, fmt.Sprintf("%d:%d", h, m))
} else {
ret = append(ret, fmt.Sprintf("%d:0%d", h, m))
}
}
}
}
return ret
}

36
20210114/main_test.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"reflect"
"sort"
"testing"
)
func Test_readBinaryWatch(t *testing.T) {
type args struct {
num int
}
tests := []struct {
name string
args args
want []string
}{
{
"test1",
args{
1,
},
[]string{"1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := readBinaryWatch(tt.args.num)
sort.Strings(got)
sort.Strings(tt.want)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readBinaryWatch() = %v, want %v", got, tt.want)
}
})
}
}