This commit is contained in:
VicRen
2020-11-18 10:59:53 +08:00
parent e1ab1ea28a
commit c3c3c154d7
2 changed files with 88 additions and 0 deletions

22
20201118/main.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
fmt.Println(findMaxLine(65535))
}
func findMaxLine(n int32) int32 {
var ret int32
for {
if findNumber(ret) > n {
ret--
return ret
}
ret++
}
}
func findNumber(n int32) int32 {
return n * (n + 1) / 2
}

66
20201118/main_test.go Normal file
View File

@@ -0,0 +1,66 @@
package main
import "testing"
func Test_findNumber(t *testing.T) {
type args struct {
n int32
}
tests := []struct {
name string
args args
want int32
}{
{
"1",
args{1},
1,
},
{
"2",
args{2},
3,
},
{
"4",
args{4},
10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findNumber(tt.args.n); got != tt.want {
t.Errorf("findNumber() = %v, want %v", got, tt.want)
}
})
}
}
func Test_findMaxLine(t *testing.T) {
type args struct {
n int32
}
tests := []struct {
name string
args args
want int32
}{
{
"5",
args{5},
2,
},
{
"8",
args{8},
3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findMaxLine(tt.args.n); got != tt.want {
t.Errorf("findMaxLine() = %v, want %v", got, tt.want)
}
})
}
}