AcWing 898. 数字三角形-golang
原题链接
简单
作者:
一只鱼
,
2021-03-19 22:57:36
,
所有人可见
,
阅读 325
数字三角形-golang
golang
package main
import (
"fmt"
"strings"
"math"
"strconv"
"bufio"
"os"
)
var n int
var num [][]int //第i行第j个的值
var f [][]int //第i行第j个的最大值
func main(){
fmt.Scanf("%d", &n)
num = make([][]int, n+1)
f = make([][]int, n*2)
for i := 0; i <= n; i++{
num[i] = make([]int, n+1)
f[i] = make([]int, n*2)
}
for i := 0; i <= n;i++{
for j := 0; j <= i+1; j++{
f[i][j] = math.MinInt32
}
}
scan := bufio.NewScanner(os.Stdin)
for i := 1; i <= n; i++{
scan.Scan()
data := strings.Split(scan.Text(), " ")
for j := 0; j < i; j++{
tmp, _ := strconv.Atoi(data[j])
num[i][j+1] = tmp
}
}
f[1][1] = num[1][1]
for i := 2; i <= n; i ++{
for j := 1; j <= i; j++{
f[i][j] = max(f[i-1][j-1] + num[i][j], f[i-1][j] + num[i][j])
}
}
var maxNum = math.MinInt32
for i := 1; i <= n; i++{
maxNum = max(maxNum, f[n][i])
}
fmt.Println(maxNum)
}
func max(a, b int)int{
if a >b{
return a
}
return b
}