|
package main |
|
|
|
import ( |
|
"bufio" |
|
"fmt" |
|
"log" |
|
"os" |
|
"strconv" |
|
"strings" |
|
) |
|
|
|
type Params struct { |
|
C, F, X float64 |
|
} |
|
|
|
func usage() { |
|
if len(os.Args) <= 1 { |
|
fmt.Fprintf(os.Stderr, "usage: %s [inputfile]\n", os.Args[0]) |
|
os.Exit(2) |
|
} |
|
} |
|
|
|
func parseFloat(s string) float64 { |
|
f, _ := strconv.ParseFloat(s, 64) |
|
return f |
|
} |
|
|
|
func main() { |
|
usage() |
|
|
|
file, err := os.Open(os.Args[1]) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer file.Close() |
|
|
|
scanner := bufio.NewScanner(file) |
|
case_id := 1 |
|
|
|
scanner.Scan() |
|
number_of_tests := parseFloat(scanner.Text()) |
|
//fmt.Println("Test: ", number_of_tests) |
|
|
|
for scanner.Scan() { |
|
line := strings.Split(scanner.Text(), " ") |
|
p := Params{ |
|
C: parseFloat(line[0]), |
|
F: parseFloat(line[1]), |
|
X: parseFloat(line[2]), |
|
} |
|
|
|
time := calculateTime(p, 0.0, 0.0, 0.0) |
|
fmt.Printf("Case #%d: %.7f\n", case_id, time) |
|
|
|
case_id++ |
|
} |
|
|
|
if case_id < int(number_of_tests) { |
|
log.Fatal("Number of tests not reached ->", case_id, number_of_tests) |
|
} |
|
} |
|
|
|
func calculateTime(p Params, i, prev_farm, prev_goal float64) float64 { |
|
f := 2.0 + p.F*i |
|
farm := p.C / f |
|
goal := p.X / f |
|
// fmt.Println(p) |
|
// fmt.Println("i:", i, "f:", f, "farm:", farm, "goal:", goal, "prev_farm:", prev_farm, "prev_goal:", prev_goal, "time:", time) |
|
// fmt.Println(goal+prev_farm, "<", prev_goal) |
|
if i == 0 || goal+prev_farm < prev_goal { |
|
return prev_farm + calculateTime(p, i+1.0, farm, goal) |
|
} else { |
|
return prev_goal |
|
} |
|
} |