go语言基础之切片做函数参数
1、切片做函数参数 (备注:用了冒泡排序)
示例:
package main //必须有个main包 import "fmt"
import "math/rand"
import "time" 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)
}
执行结果:
排序前: [91 20 62 5 47 51 97 54 72 18]
排序后: [5 18 20 47 51 54 62 72 91 97]
go语言基础之切片做函数参数的更多相关文章
- go语言基础之数组做函数参数是值拷贝
1.数组做函数参数是值拷贝 示例: package main //必须有个main包 import "fmt" //数组做函数参数,它是值传递 //实参数组的每个元素给形参数组拷贝 ...
- go语言基础之指针做函数参数用地址传递
1.指针做函数参数 示例: package main //必须有个main包 import "fmt" func swap(p1, p2 *int) { *p1, *p2 = *p ...
- go语言基础之指针做函数参数
1.指针做函数参数 示例: package main //必须有个main包 import "fmt" func swap(a, b int) { a, b = b, a fmt. ...
- go语言基础之结构体做函数参数 值传递和地址传递
1.结构体做函数参数值传递 示例: package main //必须有个main包 import "fmt" //定义一个结构体类型 type Student struct { ...
- go语言基础之map赋值、遍历、删除 、做函数参数
1.map赋值 示例: package main //必须有个main包 import "fmt" func main() { m1 := map[int]string{1: &q ...
- go语言基础之数组指针做函数参数
1.数组指针做函数参数 示例: package main //必须有个main包 import "fmt" //p指向实现数组a,它是指向数组,它是数组指针 //*p代表指针所指向 ...
- 3205: 数组做函数参数--数组元素求和1--C语言
3205: 数组做函数参数--数组元素求和1--C语言 时间限制: 1 Sec 内存限制: 128 MB提交: 178 解决: 139[提交][状态][讨论版][命题人:smallgyy] 题目描 ...
- 3204: 数组做函数参数--排序函数2--C语言
3204: 数组做函数参数--排序函数2--C语言 时间限制: 1 Sec 内存限制: 128 MB提交: 211 解决: 143[提交][状态][讨论版][命题人:smallgyy] 题目描述 ...
- 3203 数组做函数参数----排序函数--C语言版
3203: 数组做函数参数----排序函数--C语言版 时间限制: 1 Sec 内存限制: 128 MB提交: 253 解决: 151[提交][状态][讨论版][命题人:smallgyy] 题目描 ...
随机推荐
- DDR3调试记录
FPGA采集视频数据并写到DDR3,然后从DDR3读出并送给显示终端显示.不能稳定显示.但用FPGA内部逻辑产生color bar写到DDR3后读出来显示正常.因此DDR3部分逻辑没有问题 ...
- 论 异常处理机制中的return关键字
Java中,执行try-catch-finally语句需要注意: 第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂 ...
- Linux教程 - 管道和重定向
管道和重定向! 保持数据流动 介绍 在前两节中,我们看了一些可以为我们操作数据的过滤器.在本节中,我们将看到我们如何将它们结合在一起来执行更强大的数据操作. 本节涉及一些阅读.即使这些机制及其 ...
- bzoj 3252: 攻略 -- 长链剖分+贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神 ...
- Substring with Concatenation of All Words 题解
题意 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题
A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...
- SPOJ 10234. Here Be Dragons
The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the oth ...
- Codeforces Round #294 (Div. 2)C - A and B and Team Training 水题
C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...
- 磁盘满了MySQL会做什么?
最近遇到一个故障和磁盘满有关系,并且同事也发现经常有磁盘满导致操作hang住无响应的情况,于是抽时间研究了一下这2种情况. 一.磁盘满了之后MySQL会做什么? 我们看下官方的说法 When a di ...
- pkgadd 软件安装二种方法
安装文件 pkgadd -d /soft/top-3.6.1-sol10-x86-local 安装目录文件: pkgadd -d ./ SUNWi1cs SUNWi15cs