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. 30
      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. 9
      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

@ -215,3 +215,33 @@ func TestEx19(t *testing.T) {
fmt.Println(Ex19(10))
fmt.Println(Ex19(100))
}
func TestEx20(t *testing.T) {
matrix := Ex20(5)
_printMatrix(matrix)
}
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

@ -14,15 +14,6 @@ func TestWork1(t *testing.T) {
fmt.Println()
}
func _printMatrix(matrix [][]int) {
for _, row := range matrix {
for _, element := range row {
fmt.Printf("%d\t", element)
}
fmt.Println()
}
}
func TestWork2(t *testing.T) {
matrix := Work2([]int{5, 7, 4, 8, 9, 1})
_printMatrix(matrix)

Loading…
Cancel
Save