码迷,mamicode.com
首页 > 编程语言 > 详细

golang 数组反转

时间:2017-05-11 17:00:12      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:can   pac   用例   slice   traints   etc   ffffff   测试   ++   

我做hackerearth上题目记录,具体的题目描述是这样的:

Given the size and the elements of array A, print all the elements in reverse order.

Input: 
First line of input contains, N - size of the array. 
Following N lines, each contains one integer, i{th} element of the array i.e. A[i].

Output: 
Print all the elements of the array in reverse order, each element in a new line.

Constraints:

    • 1N1001≤N≤100
    • 0A[i]1000

 输入

5
4
12
7
15
9


输出
9
15
7
12
4
上面就是个测试用例:
下面是golang代码

package main

import "fmt"

func main() {

var inputCount int
fmt.Scanln(&inputCount)
var arr= make([]int,inputCount,inputCount)
for i:=0;i<inputCount;i++{
fmt.Scanln(&arr[i])
}

for i:= inputCount-1; i>=0;i--{
fmt.Println(arr[i])
}

}

其中 fmt.Scanln 是获取第一行的输入并赋值给变量inputCount,inputCount 是一共输入数字的个数,下来就是创建一个同等大小的slice来遍历输入的数值并存储
下来就比较简单了,只是把数组反转输出
之前没有用过
fmt.Scanln 发现在一些学习算法的网站hackerrank,hackerearth 用的挺多的。下来就是要联系算法的童鞋可以到这两个网站上去学习

golang 数组反转

标签:can   pac   用例   slice   traints   etc   ffffff   测试   ++   

原文地址:http://www.cnblogs.com/jfliuyun/p/6841345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!