diff --git "a/homework/week1/Go12008\345\220\264\346\266\233/main.go" "b/homework/week1/Go12008\345\220\264\346\266\233/main.go" new file mode 100644 index 0000000000000000000000000000000000000000..5a1bada98bfc9ddefe78b9ee5adc736a6111b78d --- /dev/null +++ "b/homework/week1/Go12008\345\220\264\346\266\233/main.go" @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "math/rand" + "strings" + "time" +) + +func main1() { + var a string + var err error + fmt.Println("Plz input your string:") + _, err = fmt.Scanf("%s", &a) + if err != nil { + panic(err) + } + a = strings.TrimSpace(a) + var mod int + ran := 9999 + for _, v := range a { + mod = mod + int(v)%ran + } + mod = mod % ran + fmt.Println(mod) +} + +func main2() { + for i := 1; i < 10; i++ { + for j := 1; j <= i; j++ { + //tab缩进分割 + // fmt.Printf("%d * %d = %d\t", j, i, j*i, " ") + //格式化字符串,赋值给变量s + s := fmt.Sprintf("%d*%d=%d", j, i, j*i) + //格式化输出变量s,控制s的输出宽度即可 + fmt.Printf("%-7s", s) + // fmt.Println(s) + } + fmt.Println("") + } + fmt.Println("=====================================================") + sum, mul, n, even, odd := 0, 0, 0, 0, 0 + for r1 := 0; ; { + //控制随机数的个数,达到个数后退出循环 + if n >= 20 { + break + } + //循环获取随机数,纳秒+循环次数作为种子 + src := rand.NewSource(time.Now().UnixNano() + int64(n)) + gen := rand.New(src) + //生成[0,20)的随机数 + r1 = gen.Intn(20) + //判断随机数是[0,20)的随机数,如果不是再次循环 + if r1 <= 0 { + continue + } + + if r1%2 == 0 { + even += 1 + fmt.Printf("Random even number is :%d\n", r1) + //随机数为偶数时,求乘法 + if mul == 0 { + mul = 1 + } + mul *= r1 + } else { + odd += 1 + fmt.Printf("Random odd number is :%d\n", r1) + //随机数为奇数时,求加法 + sum += r1 + } + // fmt.Printf("Random number is :%d\n", r1) + n++ + } + fmt.Printf("count of Random number is :%d\n", n) + fmt.Printf("count of even Random number is :%d\n", even) + fmt.Printf("count of odd Random number is :%d\n", odd) + fmt.Printf("Random number sum & mul result is %d,%d\n", sum, mul) + fmt.Println("=====================================================") + //使用循环 + for start1, start2 := 0, 1; ; { + if start1 == 0 { + fmt.Printf("Fibonacci sequence nubmer is:%d\n", start1) + fmt.Printf("Fibonacci sequence nubmer is:%d\n", start2) + } + result := start1 + start2 + if result > 100 { + break + } + fmt.Printf("Fibonacci sequence nubmer is:%d\n", result) + start1, start2 = start2, result + } + fmt.Println("=====================================================") + //使用slice + array := [2]int{0, 1} + slice := array[:] + for { + result := slice[len(slice)-1] + slice[len(slice)-2] + if result > 100 { + break + } + slice = append(slice, result) + } + fmt.Printf("Fibonacci sequence nubmer slice is:%v\n", slice) +} + +func main() { + fmt.Println("hello world!") +}