Repository navigation
Expand file tree
/
Copy pathtask.go
More file actions
131 lines (108 loc) · 2.07 KB
/
Copy pathtask.go
File metadata and controls
131 lines (108 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
// Зачада F. Стек - Max
// ID посылки: 66373456
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type command struct {
Name string
Step int
}
func solution(n int, commands []command) []string {
var stack []int
var result []string
getMaxStack := func() string {
if len(stack) == 0 {
return "None"
}
var m int
for i, v := range stack {
if i == 0 || v > m {
m = v
}
}
return fmt.Sprint(m)
}
for i := 0; i < n; i++ {
switch commands[i].Name {
case "get_max":
result = append(result, getMaxStack())
break
case "push":
stack = append(stack, commands[i].Step)
break
case "pop":
if len(stack) > 0 {
stack = stack[:len(stack)-1]
} else {
result = append(result, "error")
}
break
default:
break
}
}
return result
}
func main() {
n, commands, err := getInputData()
if err != nil {
showError(err)
}
// решение
result := solution(n, commands)
if len(result) > 0 {
for i := 0; i < len(result); i++ {
fmt.Println(result[i])
}
}
}
// getInputData парсинг входных данных
func getInputData() (n int, commands []command, err error) {
inp, err := getInputFromFile()
if err != nil {
return
}
defer func(inp *os.File) {
_ = inp.Close()
}(inp)
reader := bufio.NewReader(inp)
strNum, _, _ := reader.ReadLine()
n, err = strconv.Atoi(string(strNum))
if err != nil {
return
}
commands = make([]command, 0, n+1)
var num int
var com command
for i := 0; i < n; i++ {
strNums, _ := reader.ReadString('\n')
comStr := strings.Split(strings.TrimSpace(strNums), " ")
com = command{
Name: comStr[0],
}
if len(comStr) == 2 {
num, _ = strconv.Atoi(comStr[1])
com.Step = num
}
commands = append(commands, com)
}
defer reader.Reset(reader)
return
}
// getInputFromFile получение input из файла
func getInputFromFile() (*os.File, error) {
file, err := os.Open("input.txt")
if err != nil {
return nil, err
}
return file, nil
}
// showError вывод ошибки
func showError(err error) {
panic(err)
}