You are viewing a single comment's thread. Return to all comments →
Hi. This is my GO solution below, but it doesn't pass the test cases from 3 to last.
It's surely correct the way culculation I think, so I'm confused about it.
What is wrong with this solution? Significant figure??
package main import ( "bufio" "fmt" "math" "os" "sort" "strconv" ) func nextInt() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func swap(array []int) []int { var newArray []int for i := range array { if i % 2 == 0 { newArray = append(newArray, array[i]) } } sort.Ints(array) // array2を昇順に for i := range array { if len(array) % 2 == 0 { if i % 2 == 0 { newArray = append(newArray, array[i]) } } else { if i % 2 == 1 { newArray = append(newArray, array[i]) } } } return newArray } var sc = bufio.NewScanner(os.Stdin) func main() { sc.Split(bufio.ScanWords) r, n := nextInt(), nextInt() var radii[]int for i := 0; i < n; i ++ { radii = append(radii, nextInt()) } sort.Sort(sort.Reverse(sort.IntSlice(radii))) // radiiを降順に if n == 1 { fmt.Println(r * 2000) } else if n == 2 { isDistance := math.Sqrt(float64(4 * r * (radii[0] + radii[1] - r))) otherRadii := float64(radii[0] + radii[1]) ans := (isDistance + otherRadii) * 1000 fmt.Println(math.Floor(ans + .5)) } else { newArray := swap(radii) var isTotalDistance float64 for i := 0; i < len(newArray) - 1; i++ { isTotalDistance += math.Sqrt(float64(4 * r * (newArray[i] + newArray[i + 1] - r))) } otherRadii := float64(newArray[0] + newArray[len(newArray) - 1]) ans := (isTotalDistance + otherRadii) * 1000 fmt.Println(math.Floor(ans + .5)) } }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #222: Sphere Packing
You are viewing a single comment's thread. Return to all comments →
Hi. This is my GO solution below, but it doesn't pass the test cases from 3 to last.
It's surely correct the way culculation I think, so I'm confused about it.
What is wrong with this solution? Significant figure??