在acm模式下 使用GOLANG刷题的细节

在acm模式下 使用GOLANG刷题的细节,第1张

第一行是m,n的终端输入的矩阵
2-n+1行是矩阵的具体内容
方法一:
采用scan和scanln
scan遇到空格或者回车停止
scanln遇到回车才停止支持输入多个参数

package main

import (
	"fmt"
)

func main() {
	var m, n int
	fmt.Scanln(&m, &n)

	res := make([][]int, m)
	for i := range res {
		res[i] = make([]int, n)
	}
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			fmt.Scan(&res[i][j])
		}
	}
	fmt.Println(res)
}

方法二:

结合bufio函数 split strconv等函数

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()//读取一行内容
	m, _ := strconv.Atoi(strings.Split(input.Text(), " ")[0])
	n, _ := strconv.Atoi(strings.Split(input.Text(), " ")[1])
	res := make([][]int, m)
	for i := range res {
		res[i] = make([]int, n)
	}
	for i := 0; i < m; i++ {
		input.Scan()//读取一行内容
		for j := 0; j < n; j++ {
			res[i][j], _ = strconv.Atoi(strings.Split(input.Text(), " ")[j])
		}
	}
	fmt.Println(res)
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/994820.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存