-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10233.go
48 lines (40 loc) · 862 Bytes
/
10233.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
// UVa 10233 - Dermuba Triangle
package main
import (
"fmt"
"math"
"os"
)
var (
height = math.Cos(math.Pi / 6)
partialHeight = math.Tan(math.Pi/6) / 2
)
type point struct{ x, y float64 }
func distance(p1, p2 point) float64 {
return math.Sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y))
}
func pinPoint(n int) point {
level := int(math.Sqrt(float64(n)))
offset := n - level*level + 1
x := float64(offset-level-1) / 2
y := float64(level) * height
if offset%2 == 1 {
y += height - partialHeight
} else {
y += partialHeight
}
return point{x, y}
}
func main() {
in, _ := os.Open("10233.in")
defer in.Close()
out, _ := os.Create("10233.out")
defer out.Close()
var n, m int
for {
if _, err := fmt.Fscanf(in, "%d%d", &n, &m); err != nil {
break
}
fmt.Fprintf(out, "%.3f\n", distance(pinPoint(n), pinPoint(m)))
}
}