-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10700.go
63 lines (54 loc) · 1.16 KB
/
10700.go
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
// UVa 10700 - Camel trading
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func multiply(toMul []string) int64 {
product := int64(1)
for _, v := range toMul {
token, _ := strconv.ParseInt(v, 10, 64)
product *= token
}
return product
}
func findMin(line string) int64 {
toAdd := strings.Split(line, "+")
for i, vi := range toAdd {
toMul := strings.Split(vi, "*")
toAdd[i] = fmt.Sprintf("%d", multiply(toMul))
}
return add(toAdd)
}
func add(toAdd []string) int64 {
var total int64
for _, v := range toAdd {
token, _ := strconv.ParseInt(v, 10, 64)
total += token
}
return total
}
func findMax(line string) int64 {
toMul := strings.Split(line, "*")
for i, vi := range toMul {
toMul[i] = fmt.Sprintf("%d", add(strings.Split(vi, "+")))
}
return multiply(toMul)
}
func main() {
in, _ := os.Open("10700.in")
defer in.Close()
out, _ := os.Create("10700.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var n int
s.Scan()
for fmt.Sscanf(s.Text(), "%d", &n); s.Scan() && n > 0; n-- {
line := s.Text()
fmt.Fprintf(out, "The maximum and minimum are %d and %d.\n", findMax(line), findMin(line))
}
}