-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path498.go
More file actions
48 lines (42 loc) · 838 Bytes
/
498.go
File metadata and controls
48 lines (42 loc) · 838 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
37
38
39
40
41
42
43
44
45
46
47
48
// UVa 498 - Polly the Polynomial
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func calc(coefficients []int, x int) int {
sum, n, tmp := 0, len(coefficients), 1
for i := 0; i < n; i++ {
sum += coefficients[len(coefficients)-i-1] * tmp
tmp *= x
}
return sum
}
func main() {
in, _ := os.Open("498.in")
defer in.Close()
out, _ := os.Create("498.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var x int
for s.Scan() {
cs := strings.Fields(s.Text())
coefficients := make([]int, len(cs))
for i, v := range cs {
fmt.Sscanf(v, "%d", &coefficients[i])
}
s.Scan()
xs := strings.Fields(s.Text())
for i, v := range xs {
fmt.Sscanf(v, "%d", &x)
fmt.Fprint(out, calc(coefficients, x))
if i != len(xs)-1 {
fmt.Fprint(out, " ")
}
}
fmt.Fprintln(out)
}
}