用Go,但是能有两秒的时限,简直作弊!
有着C的性能,却有两倍的超时时间哈哈哈哈(暴搜暴搜暴搜+打表降低复杂度bushi)
0. 2D矩阵需要初始化
g := make([][]int, n)
for i := range g {
g[i] = make([]int, m)
}
for i := 0; i <n;i++{
for j := 0; j < n; j++{
fmt.Fscan(reader, &g[i][j])
}
}
1. 输入一行一个 fmt
直接Scan
var a, b string
fmt.Scanln(&a)
fmt.Scanln(&b)
2. bufio
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
// read first line读第一行
scanner.Scan()
a := scanner.Text()
// read second line读第二行
scanner.Scan()
b := scanner.Text()
- bufio配合fmt.Fscan
var reader = bufio.NewReader(os.Stdin)
fmt.Fscan(reader, &n)
//读取数组(切片--动态数组slice := make([]Pair, n) type Pair struct {l int, r int})
for i := 0; i < n;i++{
fmt.Fscan(reader, &slice[i].l, &slice[i].r)
}
2D点阵图(稠密矩阵)
Input:
xx.ax
x..xx
xx..x
xxx.b
reader := bufio.NewReader(os.Stdin)
// Read rows
grid := make([][]rune, 0)
for {
line, err := reader.ReadString('\n')
if err != nil { // EOF Go显示处理错误
break
}
grid = append(grid, []rune(strings.TrimSpace(line)))
}
邻接表(稀疏图)
Input:
6
7
1 2
2 3
3 4
4 5
5 6
6 1
1 4
reader := bufio.NewReader(os.Stdin)
// Read number of nodes
line, _ := reader.ReadString('\n')
numNodes, _ := strconv.Atoi(strings.TrimSpace(line))
// Read number of edges
line, _ = reader.ReadString('\n')
numEdges, _ := strconv.Atoi(strings.TrimSpace(line))
// Read edges
edges := make([][2]int, numEdges)
for i := 0; i < numEdges; i++ {
line, _ = reader.ReadString('\n')
parts := strings.Fields(line)
edges[i][0], _ = strconv.Atoi(parts[0])
edges[i][1], _ = strconv.Atoi(parts[1])
}
后续慢慢更新
Golang 即时通讯系统 (进度..%)
Golang 即时通讯系统 (已完成)