Go语言快速读写

阅读量: searchstar 2021-04-16 11:51:10
Categories: Tags:

Go语言可以用fmt.Scanfmt.Println来读写,但是效率极低,在OJ上可能会TLE。

解决方案是使用bufio

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	in := bufio.NewReader(os.Stdin)
	read_int := func() (x int) {
		c, _ := in.ReadByte()
		for ; c < '0'; c, _ = in.ReadByte() {
		}
		for ; c >= '0'; c, _ = in.ReadByte() {
			x = x * 10 + int(c - '0')
		}
		return
	}
	// Your code here
}

例子:
https://www.luogu.com.cn/record/49546964

参考文献
https://www.luogu.com.cn/record/37561564