GO学习笔记06
2018-03-25 本文已影响18人
Q大疯zi
一、随机数和切片
1.随机数的生成
package main
import (
"math/rand"
"fmt"
"time"
)
func main() {
rand.Seed(666) //设置种子只需一次,并且每次产生的随机数都是一样的
for i := 1; i <= 5; i++ {
fmt.Println("rand = ", rand.Int()) //每次循环都生成一样的随机数
}
rand.Seed(time.Now().UnixNano()) //当前的系统时间作为种子
for i := 1; i <= 5; i++ {
fmt.Println("rand = ", rand.Intn(100)) //生产100以内的随机数,并且每次不一样
}
}
rand = 4343637058903381868
rand = 3769183255805726892
rand = 1923662109321608638
rand = 1818688891928401469
rand = 4144162958715305555
rand = 54
rand = 35
rand = 85
rand = 76
rand = 4
2.冒泡排序
package main
import (
"math/rand"
"time"
"fmt"
)
func main() {
rand.Seed(time.Now().UnixNano())
var a [10]int
n := len(a)
for i := 0; i < n; i++ {
a[i] = rand.Intn(100)
fmt.Printf("%d, ", a[i])
}
fmt.Println("\n")
for i := 0; i < n-1; i++ {
for j := 0; j < n-1-i; j++ {
if a[j] > a[j+1] {
a[j], a[j+1] = a[j+1], a[j]
}
}
}
fmt.Println("---------")
for i := 0; i < n; i++ {
fmt.Printf("%d, ", a[i])
}
fmt.Printf("\n")
}
99, 10, 14, 7, 14, 27, 57, 20, 50, 41,
---------
7, 10, 14, 14, 20, 27, 41, 50, 57, 99,
3.数组的值传递和地址传递
- 值传递
package main
import "fmt"
func main() {
a := [5]int{1, 2, 3, 4, 5}
modify(a)
fmt.Println("main: a = ", a)
}
func modify(ints [5]int) {
ints[0] = 666
fmt.Println("modify: ints =", ints)
}
modify: ints = [666 2 3 4 5]
main: a = [1 2 3 4 5]
- 地址传递
package main
import "fmt"
func main() {
a := [5]int{1, 2, 3, 4, 5}
modify(&a)
fmt.Println("main: a = ", a)
}
func modify(ints *[5]int) {
(*ints)[0] = 666
fmt.Println("modify: ints =", ints)
}
modify: ints = &[666 2 3 4 5]
main: a = [666 2 3 4 5]
4.切片slice
package main
import "fmt"
func main() {
a := []int{1, 2, 3, 4, 5}
s := a[0:3:5] //0代表从下标为0的元素开始,一共取值3-0 3个元素,容量为5
fmt.Println("s = ", s)
fmt.Println("len(s) = ", len(s))
fmt.Println("cap(s) = ", cap(s))
s = a[1:4:5] //从下标为1的元素开始取3个值,容量为5
fmt.Println("s = ", s)
fmt.Println("len(s) = ", len(s))
fmt.Println("cap(s) = ", cap(s))
}
s = [1 2 3]
len(s) = 3
cap(s) = 5
s = [2 3 4]
len(s) = 3
cap(s) = 4
5.切片和数组的区别
package main
import "fmt"
func main() {
a := [5]int{}
fmt.Printf("len = %d, cap = %d\n", len(a), cap(a))
s := []int{}
fmt.Printf("切片: len = %d , cap = %d\n", len(s), cap(s))
s = append(s, 999) //给切片末尾追加一个成员
fmt.Printf("切片append后: len = %d , cap = %d\n", len(s), cap(s))
}
len = 5, cap = 5
切片: len = 0 , cap = 0
切片append后: len = 1 , cap = 1
6.切片的定义
package main
import "fmt"
func main() {
s := []int{1, 2, 3, 4}
fmt.Println("s = ", s)
//借助make函数,格式 male(切片类型,长度,容量)
s1 := make([]int, 5, 34)
fmt.Printf("len = %d, cap = %d\n", len(s1), cap(s1))
//没有指定容量,容量和长度一样
s2 := make([]int, 5)
fmt.Printf("len = %d, cap = %d\n", len(s2), cap(s2))
}
func main01() {
a := [5]int{}
fmt.Printf("len = %d, cap = %d\n", len(a), cap(a))
s := []int{}
fmt.Printf("切片: len = %d , cap = %d\n", len(s), cap(s))
s = append(s, 999) //给切片末尾追加一个成员
fmt.Printf("切片append后: len = %d , cap = %d\n", len(s), cap(s))
}
s = [1 2 3 4]
len = 5, cap = 34
len = 5, cap = 5
7.切片的截取
package main
import (
"fmt"
)
func main() {
array := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
s1 := array[:] //不指定,长度和容量一样
fmt.Println("s1 = ", s1)
fmt.Printf("len = %d, cap = %d\n", len(s1), cap(s1))
//操作某个元素
data := array[1]
fmt.Println("data = ", data)
s2 := array[3:6:7]
fmt.Println("s2 = ", s2)
fmt.Printf("len = %d, cap = %d\n", len(s2), cap(s2))
s3 :=array[:6] //[0,6)
fmt.Println("s3 = ", s3)
fmt.Printf("len = %d, cap = %d\n", len(s3), cap(s3))
s4 :=array[4:]
fmt.Println("s4= ", s4)
fmt.Printf("len = %d, cap = %d\n", len(s4), cap(s4))
}
s1 = [1 2 3 4 5 6 7 8 9]
len = 9, cap = 9
data = 2
s2 = [4 5 6]
len = 3, cap = 4
s3 = [1 2 3 4 5 6]
len = 6, cap = 9
s4= [5 6 7 8 9]
len = 5, cap = 5
8.切片append的规则
package main
import "fmt"
func main() {
//如果超过了原来的容量,通常以2倍的容量扩容
s := make([]int, 0, 1)
oldCap := cap(s)
for i := 0; i < 8; i++ {
s = append(s, i)
if newCap := cap(s); oldCap < newCap {
fmt.Printf("cap: %d====> %d\n", oldCap, newCap)
oldCap = newCap
}
}
}
cap: 1====> 2
cap: 2====> 4
cap: 4====> 8
9.copy的使用
package main
import "fmt"
func main() {
srcSlice := []int{1, 2}
dstSlice := []int{6, 6, 6, 6, 6}
copy(dstSlice, srcSlice)
fmt.Println("dst = ", dstSlice)
}
dst = [1 2 6 6 6]
10.切片作为函数参数
package main
import (
"math/rand"
"time"
"fmt"
)
func InitData(s []int) {
rand.Seed(time.Now().UnixNano()) //设置种子
for i := 0; i < len(s); i++ {
s[i] = rand.Intn(100) //产生100以内的随机数
}
}
func BubbleSort(s []int) {
n := len(s)
for i := 0; i < n-1; i++ {
for j := 0; j < n-1-i; j++ {
if s[j] > s[j+1] {
s[j], s[j+1] = s[j+1], s[j]
}
}
}
}
func main() {
n := 10
//创建一个切片,len为n
s := make([]int, n)
InitData(s)
fmt.Println("排序前=", s)
BubbleSort(s)
fmt.Println("排序后=", s)
}
排序前= [88 38 48 41 16 24 33 72 88 34]
排序后= [16 24 33 34 38 41 48 72 88 88]
11.猜数字的游戏
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var randNum int
CreatNum(&randNum)
fmt.Println("randNum: ", randNum)
randSlice := make([]int, 4)
GetNum(randSlice, randNum)
fmt.Println("randslice:", randSlice)
OnGame(randSlice)
}
func OnGame(randSlice []int) {
var num int
keySlice := make([]int, 4)
for {
for {
fmt.Printf("请输入一个四位数")
fmt.Scan(&num)
if 999 < num && num < 10000 {
break
}
fmt.Println("输入的数不符合要求")
}
//fmt.Println("num =", num)
GetNum(keySlice, num)
//fmt.Println("keySlice: ", keySlice)
n := 0
for i := 0; i < 4; i++ {
if keySlice[i] > randSlice[i] {
fmt.Printf("第%d位大了\n", i+1)
} else if keySlice[i] < randSlice[i] {
fmt.Printf("第%d位小了\n", i+1)
} else {
fmt.Printf("第%d猜对了\n", i+1)
n++
}
}
if n == 4 {
fmt.Println("恭喜你,全部正确")
break
}
}
}
func GetNum(s []int, num int) {
s[0] = num / 1000
s[1] = num % 1000 / 100
s[2] = num % 100 / 10
s[3] = num % 10
}
func CreatNum(p *int) {
rand.Seed(time.Now().UnixNano())
var num int
for {
num = rand.Intn(10000)
if num > 1000 {
break
}
}
*p = num
}
// randNum: 2634
// randslice: [2 6 3 4]
请输入一个四位数1234
第1位小了
第2位小了
第3猜对了
第4猜对了
请输入一个四位数5234
第1位大了
第2位小了
第3猜对了
第4猜对了
请输入一个四位数2534
第1猜对了
第2位小了
第3猜对了
第4猜对了
请输入一个四位数2634
第1猜对了
第2猜对了
第3猜对了
第4猜对了
恭喜你,全部正确