feat: ex20 ~ ex32

main
lensfrex 8 months ago
parent 8665c3f7b5
commit 7c0447f70f
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 21
      cap3/common.go
  2. 43
      cap3/ex20.go
  3. 66
      cap3/ex21.go
  4. 43
      cap3/ex22.go
  5. 18
      cap3/ex23.go
  6. 5
      cap3/ex24.go
  7. 20
      cap3/ex25.go
  8. 22
      cap3/ex26.go
  9. 40
      cap3/ex27.go
  10. 20
      cap3/ex28.go
  11. 13
      cap3/ex29.go
  12. 37
      cap3/ex30.go
  13. 34
      cap3/ex31.go
  14. 28
      cap3/ex32.go
  15. 5
      cap3/ex33.go
  16. 1
      cap3/ex34.go
  17. 1
      cap3/ex35.go
  18. 1
      cap3/ex36.go
  19. 1
      cap3/ex37.go
  20. 1
      cap3/ex38.go
  21. 1
      cap3/ex39.go
  22. 1
      cap3/ex40.go
  23. 1
      cap3/ex41.go
  24. 362
      cap3/example_test.go
  25. 5
      cap3/genfile.sh
  26. 1
      cap3/work10.go
  27. 1
      cap3/work11.go
  28. 1
      cap3/work12.go
  29. 1
      cap3/work13.go
  30. 1
      cap3/work14.go
  31. 1
      cap3/work15.go
  32. 1
      cap3/work16.go
  33. 1
      cap3/work17.go
  34. 1
      cap3/work18.go
  35. 1
      cap3/work19.go
  36. 1
      cap3/work20.go
  37. 1
      cap3/work21.go
  38. 1
      cap3/work22.go
  39. 1
      cap3/work23.go
  40. 1
      cap3/work24.go
  41. 1
      cap3/work25.go
  42. 1
      cap3/work26.go
  43. 1
      cap3/work27.go
  44. 1
      cap3/work6.go
  45. 1
      cap3/work7.go
  46. 1
      cap3/work8.go
  47. 1
      cap3/work9.go
  48. 63
      cap3/work_test.go
  49. 0
      package.sh

@ -0,0 +1,21 @@
package cap3
import "fmt"
func _printMatrix(matrix [][]int) {
for _, row := range matrix {
for _, val := range row {
fmt.Printf("%d\t", val)
}
fmt.Println()
}
}
func _makeMatrix(size int) [][]int {
matrix := make([][]int, size)
for i := range matrix {
matrix[i] = make([]int, size)
}
return matrix
}

@ -0,0 +1,43 @@
package cap3
// Ex20 趣味矩阵
func Ex20(n int) [][]int {
matrix := make([][]int, n)
for i := 0; i < len(matrix); i++ {
matrix[i] = make([]int, n)
}
for i := range n {
for j := range n {
// 对角线
if i == j || i+j == n-1 {
matrix[i][j] = 0
continue
}
// 左上部分
if i+j < n-1 {
if i < j {
// 上
matrix[i][j] = 1
} else {
// 左
matrix[i][j] = 2
}
}
// 右下边部分
if i+j > n-1 {
if i > j {
// 下
matrix[i][j] = 3
} else {
// 右
matrix[i][j] = 4
}
}
}
}
return matrix
}

@ -0,0 +1,66 @@
package cap3
// Ex21 螺旋阵
func Ex21(n int) [][]int {
matrix := _makeMatrix(n)
// 逐层往里填充
num := 1
for layer := 0; layer < n/2; layer++ {
// 方阵左边
for i := layer; i < n-layer; i++ {
matrix[i][layer] = num
num++
}
// 方阵下边
for i := layer + 1; i < n-layer; i++ {
matrix[n-layer-1][i] = num
num++
}
// 方阵右边
for i := n - layer - 2; i >= layer; i-- {
matrix[i][n-layer-1] = num
num++
}
// 方阵上边
for i := n - layer - 2; i > layer; i-- {
matrix[layer][i] = num
num++
}
}
// 奇数阶中间位置需要手动设置
if n%2 != 0 {
matrix[n/2][n/2] = n * n
}
return matrix
}
func Ex21type2(n int) [][]int {
matrix := _makeMatrix(n + 10)
row, col, t := 0, 0, 1
for i := 0; i < n*n; {
for j := 0; j < 2*n; j++ {
i++
matrix[row][col] = i
switch (j + 1) / (n + 1) {
case 0:
row += t
case 1:
col += t
}
}
n--
t = -t
}
return matrix
}

@ -0,0 +1,43 @@
package cap3
// Ex22 魔方阵,罗伯法
func Ex22(n int) [][]int {
matrix := _makeMatrix(n)
if n%2 == 0 {
return matrix
}
// 起始位置在第一行的中间
i, j := 0, n/2
for num := 1; num <= n*n; num++ {
matrix[i][j] = num // 填充数字
num++ // 下一个数字
i-- // 向上移动
j++ // 向右移动
// 如果移动后超出了上边界和右边界
if i < 0 && j > n-1 {
i += 2 // 向下移动两格
j-- // 向左移动一格
}
// 如果超出了上边界
if i < 0 {
i = n - 1 // 移动到最下面一行
}
// 如果超出了右边界
if j > n-1 {
j = 0 // 移动到最左边一列
}
// 如果当前位置已经填充了数字
if matrix[i][j] != 0 {
i += 2 // 向下移动两格
j-- // 向左移动一格
}
}
return matrix
}

@ -0,0 +1,18 @@
package cap3
type Pair struct {
first, second int
}
func Ex23(nums []int) [][]int {
countMap := _makeMatrix(10)
for _, num1 := range nums {
for _, num2 := range nums {
countMap[num1][num2]++
countMap[num2][num1]++
}
}
return countMap
}

@ -0,0 +1,5 @@
package cap3
func Ex24(n int) int {
return 0
}

@ -0,0 +1,20 @@
package cap3
func Ex25(studentScore [][]int) int {
studentCount := 0
for _, scores := range studentScore {
scoreCount := 0
for _, score := range scores {
if score >= 90 {
scoreCount++
}
if scoreCount > 3 {
studentCount++
break
}
}
}
return studentCount
}

@ -0,0 +1,22 @@
package cap3
func Ex26(n int) []int {
lights := make([]bool, n)
// 默认值false,实际上意味着第一个人已经把所有灯关了,没必要再操作一遍
// 第i个人,从第二个人开始
for i := 2; i <= n; i++ {
// 编号为i的倍数的灯状态取反
for j := 1; i*j <= n; j++ {
lights[i*j-1] = !lights[i*j-1]
}
}
result := make([]int, 0, n)
for light, on := range lights {
if on {
result = append(result, light+1)
}
}
return result
}

@ -0,0 +1,40 @@
package cap3
type Ex27Condition int
const (
Ex27ConditionMin Ex27Condition = iota
Ex27ConditionMax
)
func Ex27(nums []int) (max1, max2, min1, min2 int) {
size := len(nums)
if size < 3 {
return
}
// 最大的乘积
maxProd := nums[size-1] * nums[1]
max1, max2 = nums[size-1], nums[1]
// 最小的乘积
minProd := nums[size-1] * nums[1]
min1, min2 = nums[size-1], nums[1]
for i := 1; i < size; i++ {
first, second := (size+i-1)%size, (i+1)%size
// 当前的乘积
currentProd := nums[first] * nums[second]
// 找出最大最小的乘积
if currentProd > maxProd {
maxProd = currentProd
max1, max2 = nums[first], nums[second]
} else if currentProd < minProd {
minProd = currentProd
min1, min2 = nums[first], nums[second]
}
}
return max1, max2, min1, min2
}

@ -0,0 +1,20 @@
package cap3
func Ex28(nums []int) {
n := len(nums)
for i := 0; i < n-1; i++ {
swapped := false
for j := 0; j < n-1-i; j++ {
if nums[j] < nums[j+1] {
tmp := nums[j]
nums[j] = nums[j+1]
nums[j+1] = tmp
swapped = true
}
}
if !swapped {
break
}
}
}

@ -0,0 +1,13 @@
package cap3
func Ex29(nums []int) bool {
for _, num1 := range nums {
for _, num2 := range nums {
if num1 == num2 {
return false
}
}
}
return true
}

@ -0,0 +1,37 @@
package cap3
import "math"
type Ex30TriangleType int
const (
Ex30TriangleTypeNone Ex30TriangleType = iota // 不是三角形
Ex30TriangleTypeNormal Ex30TriangleType = iota // 普通三角形
Ex30TriangleTypeIsosceles Ex30TriangleType = iota // 等腰三角形
Ex30TriangleTypeEquilateral Ex30TriangleType = iota // 等边三角形
Ex30TriangleTypeRight Ex30TriangleType = iota // 直角三角形
)
func Ex30(nums []float64) Ex30TriangleType {
a, b, c := nums[0], nums[1], nums[2]
if a <= 0 || b <= 0 || c <= 0 || a+b <= c || a+c <= b || b+c <= a {
return Ex30TriangleTypeNone
}
if a == b && b == c {
return Ex30TriangleTypeEquilateral
}
if a == b || a == c || b == c {
return Ex30TriangleTypeIsosceles
}
if math.Pow(a, 2)+math.Pow(b, 2) == math.Pow(c, 2) ||
math.Pow(a, 2)+math.Pow(c, 2) == math.Pow(b, 2) ||
math.Pow(b, 2)+math.Pow(c, 2) == math.Pow(a, 2) {
return Ex30TriangleTypeRight
} else {
return Ex30TriangleTypeNormal
}
}

@ -0,0 +1,34 @@
package cap3
func Ex31(a, b, c int) int {
lcm, maxNum := 0, 0
maxNum = max(a, b, c)
for i := 2; i <= maxNum; i++ {
hasDivisible := true
for hasDivisible {
hasDivisible = false
if a%i == 0 {
a = a / i
hasDivisible = true
}
if b%i == 0 {
b = b / i
hasDivisible = true
}
if c%i == 0 {
c = c / i
hasDivisible = true
}
if hasDivisible {
lcm = lcm * i
}
}
maxNum = max(a, b, c)
}
return lcm
}

@ -0,0 +1,28 @@
package cap3
func Ex32() int {
for x := 1; x <= 4; x++ {
count := 0
if x != 1 {
count++
}
if x == 3 {
count++
}
if x == 4 {
count++
}
if x != 4 {
count++
}
if count == 3 {
return x
}
}
return 0
}

@ -0,0 +1,5 @@
package cap3
func Ex33() {
}

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -1,217 +1,247 @@
package cap3
import (
"fmt"
"strconv"
"testing"
"fmt"
"strconv"
"testing"
)
func TestEx1(t *testing.T) {
fmt.Println(Ex1(1))
fmt.Println(Ex1(2))
fmt.Println(Ex1(3))
fmt.Println(Ex1(4))
fmt.Println(Ex1(5))
fmt.Println(Ex1(6))
fmt.Println(Ex1(7))
fmt.Println(Ex1(8))
fmt.Println(Ex1(9))
fmt.Println(Ex1(1))
fmt.Println(Ex1(2))
fmt.Println(Ex1(3))
fmt.Println(Ex1(4))
fmt.Println(Ex1(5))
fmt.Println(Ex1(6))
fmt.Println(Ex1(7))
fmt.Println(Ex1(8))
fmt.Println(Ex1(9))
}
func TestEx2(t *testing.T) {
result := Ex2()
for num, factors := range result {
fmt.Printf("%d's factors are %+v\n", num, factors)
}
result := Ex2()
for num, factors := range result {
fmt.Printf("%d's factors are %+v\n", num, factors)
}
}
func TestEx3(t *testing.T) {
matrix := [][]int{
{1, 4, 5, 1},
{8, 9, 6, 7},
{1, 1, 4, 2},
{0, 8, 2, 9},
}
row, col, val := Ex3(matrix)
if row != -1 && col != -1 {
fmt.Printf("(%d,%d): %d\n", row, col, val)
}
matrix := [][]int{
{1, 4, 5, 1},
{8, 9, 6, 7},
{1, 1, 4, 2},
{0, 8, 2, 9},
}
row, col, val := Ex3(matrix)
if row != -1 && col != -1 {
fmt.Printf("(%d,%d): %d\n", row, col, val)
}
}
func TestEx5(t *testing.T) {
beadsNum := 5
step := 0
fmt.Println("NoneRecursive: ")
Ex5(beadsNum, AlgoTypeNoneRecursion, func(from string, to string) {
step++
fmt.Printf("step %d: %s -> %s\n", step, from, to)
})
fmt.Println("----------------")
step = 0
fmt.Println("Recursive: ")
Ex5(beadsNum, AlgoTypeRecursion, func(from string, to string) {
step++
fmt.Printf("step %d: %s -> %s\n", step, from, to)
})
beadsNum := 5
step := 0
fmt.Println("NoneRecursive: ")
Ex5(beadsNum, AlgoTypeNoneRecursion, func(from string, to string) {
step++
fmt.Printf("step %d: %s -> %s\n", step, from, to)
})
fmt.Println("----------------")
step = 0
fmt.Println("Recursive: ")
Ex5(beadsNum, AlgoTypeRecursion, func(from string, to string) {
step++
fmt.Printf("step %d: %s -> %s\n", step, from, to)
})
}
func TestEx6(t *testing.T) {
prev := 0
Ex6RecursionOutput(6, func(dividedNums []int) {
for i, num := range dividedNums {
if i == 0 {
if prev != num {
prev = num
fmt.Println()
}
fmt.Printf("%d", num)
} else {
fmt.Printf("+%d", num)
}
}
fmt.Print("\t\t\t")
})
fmt.Println()
fmt.Println("------------")
result := Ex6Recursion(6)
fmt.Println(result)
result = Ex6NoneRecursion(6)
fmt.Println(result)
prev := 0
Ex6RecursionOutput(6, func(dividedNums []int) {
for i, num := range dividedNums {
if i == 0 {
if prev != num {
prev = num
fmt.Println()
}
fmt.Printf("%d", num)
} else {
fmt.Printf("+%d", num)
}
}
fmt.Print("\t\t\t")
})
fmt.Println()
fmt.Println("------------")
result := Ex6Recursion(6)
fmt.Println(result)
result = Ex6NoneRecursion(6)
fmt.Println(result)
}
func TestEx7(t *testing.T) {
num := uint64(123456)
Ex7(num, AlgoTypeNoneRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
Ex7(num, AlgoTypeRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
num := uint64(123456)
Ex7(num, AlgoTypeNoneRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
Ex7(num, AlgoTypeRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
}
func TestEx8(t *testing.T) {
num := uint64(123456)
Ex8(num, AlgoTypeNoneRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
Ex8(num, AlgoTypeRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
num := uint64(123456)
Ex8(num, AlgoTypeNoneRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
Ex8(num, AlgoTypeRecursion, func(digit uint8) {
fmt.Printf("%d ", digit)
})
fmt.Println()
}
func TestEx9(t *testing.T) {
bits := Ex9t1(137)
fmt.Printf("%d=", 137)
for i, bit := range bits {
if bit {
fmt.Printf("2^%d + ", i)
}
}
println()
pows := Ex9t2(137)
fmt.Printf("%d=", 137)
for _, pow := range pows {
fmt.Printf("2^%d + ", pow)
}
println()
bits := Ex9t1(137)
fmt.Printf("%d=", 137)
for i, bit := range bits {
if bit {
fmt.Printf("2^%d + ", i)
}
}
println()
pows := Ex9t2(137)
fmt.Printf("%d=", 137)
for _, pow := range pows {
fmt.Printf("2^%d + ", pow)
}
println()
}
func TestEx10(t *testing.T) {
Ex10(5, 3, func(nums []int) {
fmt.Printf("%+v\n", nums)
})
Ex10(5, 3, func(nums []int) {
fmt.Printf("%+v\n", nums)
})
}
func TestEx14(t *testing.T) {
num := uint64(8734112)
Ex14StrInput(strconv.FormatUint(num, 10), func(str string) {
fmt.Printf("%s-", str)
})
fmt.Println()
Ex14NumInput(num, func(str string) {
fmt.Printf("%s-", str)
})
fmt.Println()
num := uint64(8734112)
Ex14StrInput(strconv.FormatUint(num, 10), func(str string) {
fmt.Printf("%s-", str)
})
fmt.Println()
Ex14NumInput(num, func(str string) {
fmt.Printf("%s-", str)
})
fmt.Println()
}
func TestEx15(t *testing.T) {
result := Ex15(60, 70)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 100)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 74)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 130)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result := Ex15(60, 70)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 100)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 74)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
result = Ex15(60, 130)
for cash, cashNum := range result {
fmt.Printf("%d->%d, ", cashes[cash], cashNum)
}
fmt.Println()
}
func TestEx16(t *testing.T) {
count := 0
Ex16(func(x, x2 int) {
count++
fmt.Printf("%d: %d, x^2=%d\n", count, x, x2)
})
count := 0
Ex16(func(x, x2 int) {
count++
fmt.Printf("%d: %d, x^2=%d\n", count, x, x2)
})
}
func TestEx17(t *testing.T) {
n, k := 10, 2
result := Ex17Normal(n, k)
fmt.Println(result)
n, k := 10, 2
result := Ex17Normal(n, k)
fmt.Println(result)
result = Ex17Math(n, k)
fmt.Println(result)
result = Ex17Math(n, k)
fmt.Println(result)
}
func TestEx18(t *testing.T) {
fmt.Println(Ex18("6451", "6637") == "42815287")
fmt.Println(Ex18("1111", "2") == "2222")
fmt.Println(Ex18("6451", "6637") == "42815287")
fmt.Println(Ex18("1111", "2") == "2222")
// 来个夸张点的
ans := "59431035264803873745814101793588195732295068254603339623610036212240642359886400430165726959529476600423448130231213495885200101414878403047792277609642"
fmt.Println(Ex18(
"953249582974085793083245237450927435989430572386540298743509843545728475284751234",
"62345723854798175908734905872984724974984398572942345324535728479275413") == ans,
)
result := Ex18type3([]uint8{6, 4, 5, 1}, 6637)
fmt.Println(DigitSlice2String(result) == "42815287")
}
// 来个夸张点的
ans := "59431035264803873745814101793588195732295068254603339623610036212240642359886400430165726959529476600423448130231213495885200101414878403047792277609642"
fmt.Println(Ex18(
"953249582974085793083245237450927435989430572386540298743509843545728475284751234",
"62345723854798175908734905872984724974984398572942345324535728479275413") == ans,
)
func TestEx19(t *testing.T) {
fmt.Println(Ex19(10))
fmt.Println(Ex19(100))
}
result := Ex18type3([]uint8{6, 4, 5, 1}, 6637)
fmt.Println(DigitSlice2String(result) == "42815287")
func TestEx20(t *testing.T) {
matrix := Ex20(5)
_printMatrix(matrix)
}
func TestEx19(t *testing.T) {
fmt.Println(Ex19(10))
fmt.Println(Ex19(100))
func TestEx21(t *testing.T) {
matrix := Ex21(3)
_printMatrix(matrix)
fmt.Println("----------------")
matrix = Ex21(4)
_printMatrix(matrix)
fmt.Println("================")
matrix = Ex21type2(3)
_printMatrix(matrix)
fmt.Println("----------------")
matrix = Ex21type2(4)
_printMatrix(matrix)
}
func TestEx22(t *testing.T) {
m := Ex22(3)
_printMatrix(m)
}

@ -0,0 +1,5 @@
# shellcheck disable=SC2006
for count in `seq 20 41`
do
echo 'package cap3' >> ex$count.go
done

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -0,0 +1 @@
package cap3

@ -1,64 +1,55 @@
package cap3
import (
"fmt"
"testing"
"fmt"
"testing"
)
func TestWork1(t *testing.T) {
result := Work1(100)
for _, b := range result {
fmt.Print(b)
}
result := Work1(100)
for _, b := range result {
fmt.Print(b)
}
fmt.Println()
}
func _printMatrix(matrix [][]int) {
for _, row := range matrix {
for _, element := range row {
fmt.Printf("%d\t", element)
}
fmt.Println()
}
fmt.Println()
}
func TestWork2(t *testing.T) {
matrix := Work2([]int{5, 7, 4, 8, 9, 1})
_printMatrix(matrix)
matrix := Work2([]int{5, 7, 4, 8, 9, 1})
_printMatrix(matrix)
println("----------------")
println("----------------")
matrix = Work2type2([]int{5, 7, 4, 8, 9, 1})
_printMatrix(matrix)
matrix = Work2type2([]int{5, 7, 4, 8, 9, 1})
_printMatrix(matrix)
}
func TestWork3(t *testing.T) {
matrix := Work3(7)
_printMatrix(matrix)
matrix := Work3(7)
_printMatrix(matrix)
fmt.Println("----------------")
fmt.Println("----------------")
matrix = Work3(6)
_printMatrix(matrix)
matrix = Work3(6)
_printMatrix(matrix)
}
func TestWork4(t *testing.T) {
matrix := Work4(5)
_printMatrix(matrix)
matrix := Work4(5)
_printMatrix(matrix)
}
func TestWork5(t *testing.T) {
matrix := Work5(6)
_printMatrix(matrix)
matrix := Work5(6)
_printMatrix(matrix)
fmt.Println("----------------")
fmt.Println("----------------")
matrix = Work5(5)
_printMatrix(matrix)
matrix = Work5(5)
_printMatrix(matrix)
fmt.Println("----------------")
fmt.Println("----------------")
matrix = Work5(10)
_printMatrix(matrix)
matrix = Work5(10)
_printMatrix(matrix)
}

Loading…
Cancel
Save