-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum-squares.go
executable file
·93 lines (72 loc) · 1.29 KB
/
sum-squares.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
var reader = bufio.NewReader(os.Stdin)
func sumSquare(sum []int, step int, Yn []string, index int) {
if index >= (len(Yn)) {
return
}
y, err := strconv.Atoi(Yn[index])
if err != nil {
os.Exit(1)
}
if y >= -100 && y <= 100 {
if y > 0 {
sum[step] += (y * y)
}
} else {
os.Exit(1)
}
index = index + 1
sumSquare(sum, step, Yn, index)
}
func process(sum []int, step int, N int) {
if step >= N {
return
}
textX, _ := reader.ReadString('\n')
textX = strings.TrimRight(textX, "\n")
X, err := strconv.Atoi(textX)
if err != nil {
os.Exit(1)
}
if X < 1 || X > 100 {
os.Exit(1)
}
textYn, _ := reader.ReadString('\n')
textYn = strings.TrimRight(textYn, "\n")
Yn := strings.Split(textYn, " ")
if len(Yn) != X {
os.Exit(1)
}
sumSquare(sum, step, Yn, 0)
step = step + 1
process(sum, step, N)
}
func display(sum []int, index int, N int) {
if index >= N {
return
}
fmt.Println(sum[index])
index = index + 1
display(sum, index, N)
}
func main() {
textN, _ := reader.ReadString('\n')
textN = strings.TrimRight(textN, "\n")
N, err := strconv.Atoi(textN)
if err != nil {
os.Exit(1)
}
if N < 1 || N > 100 {
os.Exit(1)
}
var sum = make([]int, N)
process(sum, 0, N)
display(sum, 0, N)
}