-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10281.go
More file actions
36 lines (31 loc) · 747 Bytes
/
10281.go
File metadata and controls
36 lines (31 loc) · 747 Bytes
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
// UVa 10281 - Average Speed
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
in, _ := os.Open("10281.in")
defer in.Close()
out, _ := os.Create("10281.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var speed, time, hr, min, sec, newSpeed int
var distance float64
for s.Scan() {
line := s.Text()
r := strings.NewReader(line)
fmt.Fscanf(r, "%d:%d:%d", &hr, &min, &sec)
newTime := hr*3600 + min*60 + sec
newDistance := float64(speed*(newTime-time)) / 3600
if strings.Contains(line, " ") {
fmt.Fscanf(r, "%d", &newSpeed)
distance, time, speed = distance+newDistance, newTime, newSpeed
} else {
fmt.Fprintf(out, "%s %.2f km\n", line, distance+newDistance)
}
}
}