diff --git a/cap3/work14.go b/cap3/work14.go index 17e9fed..10e8c1a 100644 --- a/cap3/work14.go +++ b/cap3/work14.go @@ -1 +1,22 @@ package cap3 + +import "math" + +func Work14() []int { + resultSet := make([]int, 0) + for num := 100; num <= 999; num++ { + // 开方取整,再平方判,断是否能获得原来的数,来识别是否为完全平方数 + intSqrt := int(math.Sqrt(float64(num)) + 0.5) + if intSqrt*intSqrt != num { + continue + } + + // num的百位,十位,个位数,其中有相等即可 + a, b, c := num/100, num%100/10, num%10 + if a == b || a == c || b == c { + resultSet = append(resultSet, num) + } + } + + return resultSet +} diff --git a/cap3/work15.go b/cap3/work15.go index 17e9fed..4c677b3 100644 --- a/cap3/work15.go +++ b/cap3/work15.go @@ -1 +1,33 @@ package cap3 + +func Work15() map[string]string { + // 甲队成员 + teamA := []string{"A", "B", "C"} + + // 乙队成员 + teamB := []string{"X", "Y", "Z"} + + match := map[string]string{} + + // 遍历所有可能的匹配方式 + for _, b := range teamB { + for _, a := range teamA { + // A不和X比赛 + if a == "A" && b == "X" { + continue + } + + // C不和X或Z比赛 + if a == "C" && (b == "X" || b == "Z") { + continue + } + + // 如果没人匹配,当前就作为对手匹配 + if _, exists := match[b]; !exists { + match[a] = b + } + } + } + + return match +} diff --git a/cap3/work16.go b/cap3/work16.go index 17e9fed..1a3cd8c 100644 --- a/cap3/work16.go +++ b/cap3/work16.go @@ -1 +1,18 @@ package cap3 + +func Work16(n int) []int { + result := make([]int, 0) + if n%4 == 0 { + result = append(result, 7) + } + + if n%7 == 0 { + result = append(result, 7) + } + + if n%9 == 0 { + result = append(result, 9) + } + + return result +} diff --git a/cap3/work17.go b/cap3/work17.go index 17e9fed..f53aa08 100644 --- a/cap3/work17.go +++ b/cap3/work17.go @@ -1 +1,10 @@ package cap3 + +func Work17(a, b, c int) int { + result := 70*a + 21*b + 15*c + for result > 105 { + result -= 105 + } + + return result +} diff --git a/cap3/work18.go b/cap3/work18.go index 17e9fed..db51acb 100644 --- a/cap3/work18.go +++ b/cap3/work18.go @@ -1 +1,45 @@ package cap3 + +func Work18(callback func(four, five int)) { + // 检查两个数是否由9个不同的数字组成 + checkDigitUnique := func(a, b int) bool { + // 创建一个map来跟踪每个数字是否已经出现过 + mark := map[int]bool{} + + // 检查第一个数的每一位数字 + for ; a > 0; a /= 10 { + // 如果数字已经在map中,直接返回false + if _, exists := mark[a%10]; exists { + return false + } else { + // map标记 + mark[a%10] = true + } + } + + for ; b > 0; b /= 10 { + if _, exists := mark[b%10]; exists { + return false + } else { + mark[b%10] = true + } + } + + // map中的数字总数为9,返回true + return len(mark) == 9 + } + + // 遍历所有的四位数 + for four := 1000; four <= 9999; four++ { + // 计算当前四位数的两倍 + five := 2 * four + + // 检查结果是否为五位数 + if 10000 <= five && five <= 99999 { + // 调用checkUnique函数检查数字是否互不相同,条件满足则回调 + if checkDigitUnique(four, five) { + callback(four, five) + } + } + } +} diff --git a/cap3/work19.go b/cap3/work19.go index 17e9fed..266cba0 100644 --- a/cap3/work19.go +++ b/cap3/work19.go @@ -1 +1,31 @@ package cap3 + +import ( + "math" + "strings" +) + +var ( + _hexMap = map[byte]int{ + '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, + '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, + 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, + } +) + +func Work19(hexStr string) int { + // 将输入字符串转换为大写,以便匹配映射表 + hexStr = strings.ToUpper(hexStr) + + decValue := 0 + // 从字符串最右端开始转换每个字符 + for i := 0; i < len(hexStr); i++ { + char := hexStr[len(hexStr)-1-i] + // 获取字符对应的十进制值,计算十进制 + if value, exists := _hexMap[char]; exists { + decValue += value * int(math.Pow(16, float64(i))) + } + } + + return decValue +} diff --git a/cap3/work20.go b/cap3/work20.go index 17e9fed..c76b646 100644 --- a/cap3/work20.go +++ b/cap3/work20.go @@ -1 +1,56 @@ package cap3 + +func Work20(callback func(x, y, z int)) { + // 检查x, y, z是否包含1到9的所有数字各一次,且不包含0 + checkDigit := func(x, y, z int) bool { + appearCnt := make([]int, 10) // 创建一个数组来存储数字0-9的出现次数 + + // x的各位数字 + appearCnt[x/10]++ + appearCnt[x%10]++ + + // y的各位数字 + appearCnt[y%10]++ + appearCnt[y/10%10]++ + appearCnt[y/100]++ + + // z的各位数字 + appearCnt[z%10]++ + appearCnt[z/10%10]++ + appearCnt[z/100%10]++ + appearCnt[z/1000]++ + + // 有0,不符合条件 + if appearCnt[0] != 0 { + return false + } + + // 检查1-9出现次数 + for _, cnt := range appearCnt { + if cnt != 1 { + return false + } + } + + return true + } + + // 双重循环遍历所有可能的x和y组合 + // x从12开始到98 + for x := 12; x < 99; x++ { + // y从123开始到986 + for y := 123; y < 987; y++ { + // 如果乘积超过9999,停止内层循环(太大了,已经不符合条件了) + z := x * y + if z > 9999 { + break + } + + // 检查x, y, z各位数字情况 + if checkDigit(x, y, z) { + // 回调结果 + callback(x, y, z) + } + } + } +} diff --git a/cap3/work21.go b/cap3/work21.go index 17e9fed..0619898 100644 --- a/cap3/work21.go +++ b/cap3/work21.go @@ -1 +1,41 @@ package cap3 + +import "math" + +func Work21(callback func(num, sqrt int)) { + // 检查两个数是否由9个不同的数字组成 + checkDigitUnique := func(a, b int) bool { + // 创建一个map来跟踪每个数字是否已经出现过 + mark := map[int]bool{} + + // 检查第一个数的每一位数字 + for ; a > 0; a /= 10 { + // 如果数字已经在map中,直接返回false + if _, exists := mark[a%10]; exists { + return false + } else { + // map标记 + mark[a%10] = true + } + } + + for ; b > 0; b /= 10 { + if _, exists := mark[b%10]; exists { + return false + } else { + mark[b%10] = true + } + } + + // map中的数字总数为9,返回true + return len(mark) == 9 + } + + for num1 := 100000; num1 < 1000000; num1++ { + for num2 := 100; num2 < 1000; num2++ { + if math.Sqrt(float64(num1)) == float64(num2) && checkDigitUnique(num1, num2) { + callback(num1, num2) + } + } + } +} diff --git a/cap3/work22.go b/cap3/work22.go index 17e9fed..12b9aa2 100644 --- a/cap3/work22.go +++ b/cap3/work22.go @@ -1 +1,5 @@ package cap3 + +func Work22() { + +} diff --git a/cap3/work24.go b/cap3/work24.go index 17e9fed..690bcbe 100644 --- a/cap3/work24.go +++ b/cap3/work24.go @@ -1 +1,29 @@ package cap3 + +func Work24(callback func(num1, num2, result int)) { + 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 { + if check(num1) && check(num2) && check(result) { + callback(num1, num2, result) + } + } + } + } + +} diff --git a/cap3/work_test.go b/cap3/work_test.go index 41d0535..2eae798 100644 --- a/cap3/work_test.go +++ b/cap3/work_test.go @@ -71,3 +71,20 @@ func TestWork11(t *testing.T) { result = Work11(1000) println(result) } + +func TestWork15(t *testing.T) { + match := Work15() + for a, b := range match { + fmt.Println(a, " vs ", b) + } +} + +func TestWork16(t *testing.T) { + result := Work16(123) + fmt.Print("can be devide by ") + for _, num := range result { + fmt.Printf("%d, ", num) + } + + println() +}