Browse Source

AC 2008 Qualification C

dengxinyi 6 years ago
parent
commit
9c8a19b6a2

+ 52 - 12
2008/Qualification/C/main.go

@@ -3,6 +3,7 @@ package main
 import (
 	"bufio"
 	"fmt"
+	"math"
 	"os"
 	"strconv"
 	"strings"
@@ -14,24 +15,63 @@ func readInt(s *bufio.Scanner) int {
 	return int(i)
 }
 
-func read5Float32(s *bufio.Scanner) (float32, float32, float32, float32, float32) {
+func read5Float64(s *bufio.Scanner) (float64, float64, float64, float64, float64) {
 	s.Scan()
 	n := strings.Split(s.Text(), " ")
-	n1, _ := strconv.ParseFloat(n[0], 32)
-	n2, _ := strconv.ParseFloat(n[1], 32)
-	n3, _ := strconv.ParseFloat(n[2], 32)
-	n4, _ := strconv.ParseFloat(n[3], 32)
-	n5, _ := strconv.ParseFloat(n[4], 32)
-	return float32(n1), float32(n2), float32(n3), float32(n4), float32(n5)
+	n1, _ := strconv.ParseFloat(n[0], 64)
+	n2, _ := strconv.ParseFloat(n[1], 64)
+	n3, _ := strconv.ParseFloat(n[2], 64)
+	n4, _ := strconv.ParseFloat(n[3], 64)
+	n5, _ := strconv.ParseFloat(n[4], 64)
+	return n1, n2, n3, n4, n5
 }
 
-func hitFly(scanner *bufio.Scanner) []float32 {
+func circularSegment(r, theta float64) float64 {
+	return r * r * (theta - math.Sin(theta)) / 2
+}
+
+func hitFly(scanner *bufio.Scanner) []float64 {
 	caseCnt := readInt(scanner)
+	answer := make([]float64, caseCnt)
 	for i := 0; i < caseCnt; i++ {
-		f, R, t, r, g := read5Float32(scanner)
-
+		// r fly, r outer ring, thickness, r string and gap length
+		f, R, t, r, g := read5Float64(scanner)
+		if g <= 2*f {
+			answer[i] = 1.
+			continue
+		}
+		rad := R - t - f
+		sqrRad := rad * rad
+		gridArea := (g - 2*f) * (g - 2*f)
+		area := 0.
+		for x1 := r + f; x1 < rad; x1 += g + 2*r { // 1st quadrant, x1, y1 is the coord of br
+			for y1 := r + f; y1 < rad; y1 += g + 2*r {
+				x2, y2 := x1+g-2*f, y1+g-2*f
+				if x1*x1+y1*y1 >= sqrRad {
+					continue
+				}
+				switch {
+				case x2*x2+y2*y2 <= sqrRad: // All points inside circle
+					area += gridArea
+				case x1*x1+y2*y2 >= sqrRad && x2*x2+y1*y1 >= sqrRad:
+					// Only x1, y1 inside
+					area += circularSegment(rad, math.Pi/2-math.Asin(x1/rad)-math.Asin(y1/rad))
+					area += (math.Sqrt(sqrRad-x1*x1) - y1) * (math.Sqrt(sqrRad-y1*y1) - x1) / 2
+				case x1*x1+y2*y2 >= sqrRad: // x1, y1 and x2, y1 inside
+					area += circularSegment(rad, math.Acos(x1/rad)-math.Acos(x2/rad))
+					area += ((math.Sqrt(sqrRad-x1*x1) - y1) + (math.Sqrt(sqrRad-x2*x2) - y1)) * (x2 - x1) / 2
+				case x2*x2+y1*y1 >= sqrRad: // x1, y1 and x1, y2 inside
+					area += circularSegment(rad, math.Asin(y2/rad)-math.Asin(y1/rad))
+					area += ((math.Sqrt(sqrRad-y2*y2) - x1) + (math.Sqrt(sqrRad-y1*y1) - x1)) * (y2 - y1) / 2
+				default: // All except x2, y2 inside
+					area += circularSegment(rad, math.Asin(y2/rad)-math.Acos(x2/rad))
+					area += gridArea - (x2-math.Sqrt(sqrRad-y2*y2))*(y2-math.Sqrt(sqrRad-x2*x2))/2
+				}
+			}
+		}
+		answer[i] = 1. - area/(math.Pi*R*R/4)
 	}
-	return []float32{}
+	return answer
 }
 
 func main() {
@@ -43,7 +83,7 @@ func main() {
 		test
 	)
 
-	fileType := test
+	fileType := large
 
 	fin, _ := os.Open(inputFiles[fileType])
 	defer fin.Close()

+ 99 - 0
2008/Qualification/C/result-large.out

@@ -0,0 +1,99 @@
+Case #1: 0.372383
+Case #2: 0.125051
+Case #3: 0.656765
+Case #4: 0.077148
+Case #5: 0.004237
+Case #6: 0.923796
+Case #7: 0.139141
+Case #8: 0.051313
+Case #9: 0.005979
+Case #10: 0.168208
+Case #11: 0.106575
+Case #12: 0.165993
+Case #13: 0.008298
+Case #14: 0.005977
+Case #15: 0.004060
+Case #16: 0.008449
+Case #17: 0.017903
+Case #18: 0.599077
+Case #19: 0.818137
+Case #20: 0.004076
+Case #21: 0.384280
+Case #22: 0.293160
+Case #23: 0.004008
+Case #24: 0.873997
+Case #25: 0.004227
+Case #26: 0.533734
+Case #27: 0.015370
+Case #28: 0.008117
+Case #29: 0.223215
+Case #30: 0.019063
+Case #31: 0.417852
+Case #32: 0.495402
+Case #33: 0.050107
+Case #34: 1.000000
+Case #35: 0.008621
+Case #36: 0.393037
+Case #37: 0.986303
+Case #38: 0.105894
+Case #39: 0.009590
+Case #40: 0.079445
+Case #41: 0.042811
+Case #42: 0.158488
+Case #43: 0.140062
+Case #44: 0.998763
+Case #45: 0.059207
+Case #46: 0.724720
+Case #47: 0.491175
+Case #48: 0.131382
+Case #49: 0.641442
+Case #50: 0.006132
+Case #51: 0.011212
+Case #52: 0.004084
+Case #53: 0.013354
+Case #54: 0.209965
+Case #55: 0.019440
+Case #56: 0.008503
+Case #57: 0.007445
+Case #58: 0.186334
+Case #59: 0.826329
+Case #60: 0.011815
+Case #61: 0.674092
+Case #62: 0.004032
+Case #63: 0.045092
+Case #64: 0.004129
+Case #65: 0.999398
+Case #66: 0.953621
+Case #67: 0.026490
+Case #68: 0.245310
+Case #69: 0.052596
+Case #70: 0.004000
+Case #71: 0.348395
+Case #72: 0.160780
+Case #73: 0.004060
+Case #74: 0.006278
+Case #75: 0.449442
+Case #76: 0.101923
+Case #77: 0.011783
+Case #78: 0.112310
+Case #79: 0.943978
+Case #80: 0.058360
+Case #81: 0.034712
+Case #82: 0.788789
+Case #83: 0.041220
+Case #84: 0.679241
+Case #85: 0.006821
+Case #86: 0.019052
+Case #87: 0.081869
+Case #88: 0.015961
+Case #89: 0.845216
+Case #90: 1.000000
+Case #91: 0.530284
+Case #92: 0.014949
+Case #93: 0.536403
+Case #94: 0.802096
+Case #95: 0.004679
+Case #96: 0.944380
+Case #97: 0.045758
+Case #98: 0.749937
+Case #99: 1.000000

+ 9 - 0
2008/Qualification/C/result-small.out

@@ -0,0 +1,9 @@
+Case #1: 0.015074
+Case #2: 0.005947
+Case #3: 0.035711
+Case #4: 0.002994
+Case #5: 0.000366
+Case #6: 0.113443
+Case #7: 0.004628
+Case #8: 0.010294
+Case #9: 0.000180

+ 5 - 0
2008/Qualification/C/test.out

@@ -0,0 +1,5 @@
+Case #1: 1.000000
+Case #2: 0.910015
+Case #3: 0.000000
+Case #4: 0.002371
+Case #5: 0.573972