feat: cap3 ex33 ~ ex41

main
lensfrex 7 months ago
parent 7c0447f70f
commit 687db3d3a4
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 70
      cap3/ex34.go
  2. 23
      cap3/ex35.go
  3. 19
      cap3/ex36.go
  4. 23
      cap3/ex37.go
  5. 32
      cap3/ex38.go
  6. 9
      cap3/ex39.go
  7. 17
      cap3/ex40.go
  8. 15
      cap3/ex41.go

@ -1 +1,71 @@
package cap3
import "fmt"
const (
_opAdd = '+' // 加
_opMinus = '-' // 减
_opMulti = '*' // 乘
_opDiv = '/' // 除
)
var allOperations = [4]byte{_opAdd, _opMinus, _opMulti, _opDiv}
func Ex34(x1, x2, x3, x4, x5, y int) []string {
result := make([]string, 0)
operations := make([]byte, 4)
nums := [5]int{x1, x2, x3, x4, x5}
// 一层一层尝试运算符
for _, operations[0] = range allOperations {
// 当前为除法,判断下一个数是否为0,如果是则直接跳过,下同
if operations[0] == _opDiv && nums[1] == 0 {
continue
}
for _, operations[1] = range allOperations {
if operations[1] == _opDiv && nums[2] == 0 {
continue
}
for _, operations[2] = range allOperations {
if operations[2] == _opDiv && nums[3] == 0 {
continue
}
for _, operations[4] = range allOperations {
if operations[4] == _opDiv && nums[4] == 0 {
continue
}
// 计算表达式
p, f, q := 0, 1, nums[1]
for i, op := range operations {
switch op {
case _opAdd:
p += f * q
f, q = 1, nums[i+1]
case _opMinus:
p += f * q
f, q = -1, nums[i+1]
case _opMulti:
q *= nums[i+1]
case _opDiv:
q /= nums[i+1]
}
}
// 结果等于给定的数字y,添加到结果集中
if p+f*q == y {
text := ""
for _, num := range nums {
text += fmt.Sprintf("%d %c ", num, operations)
}
text += fmt.Sprintf("%d = %d\n", nums[4], y)
result = append(result, text)
}
}
}
}
}
return result
}

@ -1 +1,24 @@
package cap3
func Ex35(boxNum int, expectWeigh, actualWeigh int) []int {
result := make([]int, 0)
// 实际值重量正常重量差 / 10
w := (expectWeigh - actualWeigh) / 10
// w转化为2的幂的和
for w != 0 {
// k是箱子编号,从0开始, t是2的幂
k, t := 0, 1
// 找到最接近w的2的k次方t,当t大于w时结束小循环
for w-t >= 0 {
t *= 2
// t是2的k次方
k++
}
result = append(result, k-1)
w -= t / 2
}
return result
}

@ -1 +1,20 @@
package cap3
func Ex36(num int) []int {
// 用另外一种写法,直接存能整除的数
divNums := make([]int, 0)
if num%3 == 0 {
divNums = append(divNums, 3)
}
if num%5 == 0 {
divNums = append(divNums, 5)
}
if num%7 == 0 {
divNums = append(divNums, 7)
}
return divNums
}

@ -1 +1,24 @@
package cap3
func Ex37(n int) []int {
var coeff func(nums []int, n int)
coeff = func(nums []int, n int) {
if n <= 0 {
return
}
// 第一个和最后一个数字肯定是1
nums[0] = 1
coeff(nums, n-1)
nums[n] = 1
for i := n - 1; i > 0; i-- {
nums[i] += nums[i-1]
}
}
nums := make([]int, n+1)
coeff(nums, n)
return nums
}

@ -1 +1,33 @@
package cap3
func Ex38(k int, nums []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
}
numsLen := len(nums)
first, gcdNumb := 0, gcd(numsLen, k)
for i := 0; i < gcdNumb; i++ {
first = nums[i]
startIdx := i
for j := 0; j < numsLen/gcdNumb; j++ {
startIdx = (startIdx + k) % numsLen
// 交换
tmp := nums[startIdx]
nums[startIdx] = first
first = tmp
}
}
}

@ -1 +1,10 @@
package cap3
func Ex39(a, b, c int) int {
result := 70*a + 21*b + 15*c
for result > 105 {
result -= 105
}
return result
}

@ -1 +1,18 @@
package cap3
func Ex40(n int) int {
var fab func(n int) int
fab = func(n int) int {
if n == 1 {
return 1
}
if n == 2 {
return 2
}
return fab(n-1) + fab(n-2)
}
return fab(n)
}

@ -1 +1,16 @@
package cap3
import "math"
func Ex41(t int) (float64, float64) {
a, b := math.Pow(3, float64(t))/4, math.Pow(3, float64(t+1))/4
if t%2 == 0 {
a -= 0.75
b += 0.75
} else {
a += 0.75
b -= 0.75
}
return a, b
}

Loading…
Cancel
Save