You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
5.1 KiB
247 lines
5.1 KiB
package cap3
|
|
|
|
import (
|
|
"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))
|
|
}
|
|
|
|
func TestEx2(t *testing.T) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func TestEx10(t *testing.T) {
|
|
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()
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func TestEx16(t *testing.T) {
|
|
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)
|
|
|
|
result = Ex17Math(n, k)
|
|
fmt.Println(result)
|
|
}
|
|
|
|
func TestEx18(t *testing.T) {
|
|
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")
|
|
}
|
|
|
|
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)
|
|
}
|
|
|