惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
H
Help Net Security
I
InfoQ
月光博客
月光博客
量子位
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
D
DataBreaches.Net
宝玉的分享
宝玉的分享
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理

博客园 - 留云

dotnet core 自定义配置文件 Dotnet Core Windows Service golang 用tar打包文件或文件夹 golang 最和谐的子序列 golang 轮训加密算法 golang map golang 队列 golang 栈操作 golang 数组反转 nginx 在windows平台上对asp.net做反向代理 - 留云 树形结构应用技巧 用批命令更新数据库 c# 中Exception 的重试机制 jquery 调用wcf project HTTP协议之Session和Cookie C#多线程同步 SQL优化-索引 UML在关系型数据库设计中的应用 Web 应用的 UML 建模与 .NET 框架开发
golang 多维数组
留云 · 2017-05-11 · via 博客园 - 留云

具体的题目如下:(就是将多维数组的行列互换)

A multi-dimensional array is an array of arrays. 2-dimensional arrays are the most commonly used. They are used to store data in a tabular manner.

Consider following 2D array, which is of the size 3×53×5. For an array of size N×MN×M, the rows and columns are numbered from 00 to N1N−1 and columns are numbered from 00 to M1M−1, respectively. Any element of the array can be accessed by arr[i][j]arr[i][j] where 0i<N0≤i<N and 0j<M0≤j<M. For example, in the following array, the value stored at arr[1][3]arr[1][3] is 1414.

enter image description here

golang 代码如下:

其中定义二维数组没有什么复杂的,在赋值的过程中我们需要先定义一个一维数组 carray := make([]int, column, column),然后在赋值给外面的数组 array[i] =carray

下来就是字符串拼接和其他的语言不太一样,先定义一个var buffer bytes.Buffer, 然后写数据buffer.WriteString("ddd"),最后就是输出 buffer.String()

还有就是一些数值和字符的互相转换用到了包strconv

package main

import "fmt"
import "strconv"
import "bytes"


func main() {
  var row,column int
  fmt.Scanln(&row,&column)
  var array = make([][]int,row,row)
  for i:=0;i<row;i++{
      carray := make([]int, column, column)
      for j:=0;j<column;j++{
      var columnValue string
      fmt.Scan(&columnValue)
      //fmt.Println(columnValue)
      intv,_:= strconv.Atoi(columnValue)
      carray[j] = intv
      }
      array[i] =carray
  }
  //fmt.Println(array)
  var buffer bytes.Buffer 
  for i:=0;i<column;i++{
      buffer.Reset()
      for j:=0;j<row;j++{
      buffer.WriteString(strconv.Itoa(array[j][i])+" ")
      }
      fmt.Println(buffer.String())
  }
}