patch: cap3 work补充

main
lensfrex 7 months ago
parent 1d004d5f8c
commit 389867336d
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 10
      cap3/work22.go
  2. 9
      cap3/work23.go
  3. 32
      cap3/work25.go
  4. 4
      cap3/work26.go
  5. 10
      cap3/work27.go
  6. 17
      cap3/work_test.go

@ -1,5 +1,13 @@
package cap3 package cap3
func Work22() { func Work22(nums []int) int {
size := len(nums)
maxSum := 0
for i := size; i < size*2; i++ {
i1, i2, i3, i4 := (i-1)%size, i%size, (i+2)%size, (i+2)%size
sum := nums[i1] + nums[i2] + nums[i3] + nums[i4]
maxSum = max(maxSum, sum)
}
return maxSum
} }

@ -1 +1,10 @@
package cap3 package cap3
func Work23(n int) []int {
digits := make([]int, 0, 16)
for ; n > 0; n /= 10 {
digits = append(digits, n%10)
}
return digits
}

@ -1 +1,33 @@
package cap3 package cap3
func Work25(callback func(num1, num2, num3, num4, result int)) {
// 类似work24
check := func(x int) bool {
for ; x != 0; x /= 10 {
n := x % 10
// 如果有一个数字不是素数,直接false
if n != 2 && n != 3 && n != 5 && n != 7 {
return false
}
}
return true
}
// num1三位数,num2一位数,遍历查找
for num1 := 100; num1 <= 9999; num1++ {
for num2 := 1; num2 <= 0; num2++ {
// 乘出来的结果应该是四位数
result := num1 * num2
if 1000 <= result && result <= 9999 {
// 第二个乘数的两个数字位和中间的两个相乘结果
num2Digit1, num2Digit2 := num2%10, num2/10
num3, num4 := num2Digit1*num1, num2Digit2*num1
if check(num1) && check(num2) && check(num3) && check(num4) && check(result) {
callback(num1, num2, num3, num4, result)
}
}
}
}
}

@ -1 +1,5 @@
package cap3 package cap3
func Work26(m, n int) {
}

@ -1 +1,11 @@
package cap3 package cap3
var _monthNames = []string{
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December",
}
func Work27(n int) string {
return _monthNames[n-1]
}

@ -88,3 +88,20 @@ func TestWork16(t *testing.T) {
println() println()
} }
func TestWork23(t *testing.T) {
digits := Work23(123456)
fmt.Printf("%d digits\n", len(digits))
for i := len(digits) - 1; i >= 0; i-- {
print(digits[i])
}
println()
for _, digit := range digits {
print(digit)
}
println()
}

Loading…
Cancel
Save