菜鸟 只a了两道比较简单的
第一题
思路:单调栈
package main import "fmt" func main() { var n, x int fmt.Scan(&n) var stack []int //stack := make([]int, n) //fmt.Scan(&x) for i := 0; i < n; i++ { fmt.Scan(&x) for len(stack) != 0 && x > stack[len(stack)-1] { stack = stack[:len(stack)-1] } stack = append(stack, x) } fmt.Println(len(stack)) fmt.Print(stack[0]) for j := 1; j < len(stack); j++ { fmt.Printf(" %d", stack[j]) } fmt.Println(stack) }
第二题:无思路
第三题:
package main import ( "bufio" "fmt" "os" "strings" ) var m = map[string]string{} func main() { var n int fmt.Scan(&n) input := bufio.NewReader(os.Stdin) for i := 0; i < n; i++ { line, _, _ := input.ReadLine() s := string(line) str := strings.Fields(s) //if str[0] == "Change" { // Change(str[1], str[2], str[3]) //} else if str[0] == "Add" { // Adds(str[1], str[2], str[3]) //} else if str[0] == "Delete" { // Delete(str[1]) //} else if str[0] == "Query" { // Query(str[1]) //} //优化成switch语句 switch str[0] { case "Change": Change(str[1], str[2], str[3]) case "Add": Adds(str[1], str[2], str[3]) case "Delete": Delete(str[1]) case "Query": Query(str[1]) } } } func Change(s, x, y string) { if m[s] != "" { m[s] = x + " " + y } } func Delete(s string) { if m[s] != "" { delete(m, s) } } func Adds(s, x, y string) { m[s] = x + " " + y } func Query(s string) { if m[s] == "" { fmt.Println("no") } else { fmt.Println("yes") fmt.Println(m[s]) } }
全部评论
(3) 回帖