feat: cap3 work7 ~ work12

main
lensfrex 7 months ago
parent 687db3d3a4
commit bee5ef9913
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 33
      cap3/work10.go
  2. 12
      cap3/work11.go
  3. 23
      cap3/work12.go
  4. 5
      cap3/work13.go
  5. 95
      cap3/work6.go
  6. 17
      cap3/work7.go
  7. 11
      cap3/work8.go
  8. 27
      cap3/work9.go
  9. 18
      cap3/work_test.go

@ -1 +1,34 @@
package cap3 package cap3
// Work10 狼找兔子,返回兔子是否有幸免的机会,有则返回可能的洞穴
func Work10(m, n int) (bool, []int) {
gcd := func(m, n int) int {
a, b := m, n
if m < n {
a, b = n, m
}
r := a % b
for r != 0 {
a = b
b = r
r = a % b
}
return b
}
r := gcd(m, n)
if r == 1 {
return false, nil
}
safeHoles := make([]int, 0)
for i := 0; i < n; i++ {
if i%r != 0 {
safeHoles = append(safeHoles, i)
}
}
return true, safeHoles
}

@ -1 +1,13 @@
package cap3 package cap3
// Work11 求n!结果中0的数量
func Work11(n int) int {
count := 0
for i := 1; i <= n; i++ {
for num := i; num%5 == 0; num /= 5 {
count++
}
}
return count
}

@ -1 +1,24 @@
package cap3 package cap3
func Work12() []int {
size := 52
cardStatus := make([]bool, 52)
start, count := 2, 0
for count <= 104 {
for j := start; j < size; j++ {
if j%start == 0 {
cardStatus[j] = !cardStatus[j]
count++
}
}
}
result := make([]int, 0)
for i, faceDown := range cardStatus {
if !faceDown {
result = append(result, i)
}
}
return result
}

@ -1 +1,6 @@
package cap3 package cap3
//func Work13() map[string]int {
// students := []string{"A", "B", "C", "D", "E"}
//
//}

@ -1 +1,96 @@
package cap3 package cap3
import (
"fmt"
"strings"
)
//
//func Work6(input string) string {
//
//}
var before bool = false
func printString(str string) {
temp := ""
for i := 0; i < len(str); i++ {
if i == 0 {
before = false
} else {
if str[i-1] == '*' || str[i-1] == '/' {
before = true
} else {
before = false
}
}
if str[i] == '(' {
temp = getString(str[i:])
if i == 0 {
str = temp
} else {
str = str[:i] + temp
}
}
}
fmt.Println(str)
}
func getString(str string) string {
result := str
temp := ""
i := 0
length := 0
add := false
for i = 1; i < len(str); {
if str[i] == '+' || str[i] == '-' {
add = true
}
if str[i] == '(' {
temp = getString(str[i:])
length = len(temp) + len(str[:i])
str = str[:i] + temp
if length != len(result) {
if i > 1 {
i--
}
} else {
i = strings.Index(str, ")") + 1
}
result = str
continue
}
if str[i] == ')' {
break
}
i++
}
result = str
if before {
return result
}
if !add {
if i+1 < len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
} else if i+1 == len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
} else {
if i+1 < len(str) {
if str[i+1] != '*' && str[i+1] != '/' {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
} else if i+1 == len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
}
return result
}

@ -1 +1,18 @@
package cap3 package cap3
func Work7(m, n int) int {
var ack func(m, n int) int
ack = func(m, n int) int {
if m == 0 {
return n + 1
}
if n == 0 {
return ack(m-1, 1)
}
return ack(m-1, ack(m, n-1))
}
return ack(m, n)
}

@ -1 +1,12 @@
package cap3 package cap3
func Work8(input string) bool {
length := len(input)
for i := 0; i < length/2; i++ {
if input[i] != input[length-1-i] {
return false
}
}
return true
}

@ -1 +1,28 @@
package cap3 package cap3
// Work9 蚂蚁爬房,求a~b路线数,动规解法,时间O(n),空间O(1)
func Work9(a, b int) int {
if a > b {
return 0
}
length := b - a
if length <= 2 {
return 1
}
// 从a开始到后两个房间路线分别有一个和两种路线
//a+1:(a->a+1),a+2:(a->a+1->a+2, a->a+2)
prev, prev2 := 1, 2
step := 0
// 前两步已经走过了,所以len-2
for i := 0; i < length-2; i++ {
step = prev + prev2
// 上两个变成上一个,上一个变成下一个
prev2 = prev
prev = step
}
return step
}

@ -53,3 +53,21 @@ func TestWork5(t *testing.T) {
matrix = Work5(10) matrix = Work5(10)
_printMatrix(matrix) _printMatrix(matrix)
} }
func TestWork9(t *testing.T) {
result := Work9(1, 2)
println(result)
result = Work9(3, 6)
println(result)
}
func TestWork11(t *testing.T) {
result := Work11(4)
println(result)
result = Work11(60)
println(result)
result = Work11(1000)
println(result)
}

Loading…
Cancel
Save