-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10922.go
49 lines (43 loc) · 797 Bytes
/
10922.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
// UVa 10922 - 2 the 9s
package main
import (
"fmt"
"os"
"strconv"
)
func digitSum(num string) int {
var total int
for i := range num {
total += int(num[i] - '0')
}
return total
}
func nineDegree(num int) int {
var degree int
for num%9 == 0 {
degree++
newNum := digitSum(strconv.Itoa(num))
if newNum == num {
break
}
num = newNum
}
return degree
}
func main() {
in, _ := os.Open("10922.in")
defer in.Close()
out, _ := os.Create("10922.out")
defer out.Close()
var line string
for {
if fmt.Fscanf(in, "%s", &line); line == "0" {
break
}
if degree := nineDegree(digitSum(line)); degree == 0 {
fmt.Fprintf(out, "%s is not a multiple of 9.\n", line)
} else {
fmt.Fprintf(out, "%s is a multiple of 9 and has 9-degree %d.\n", line, degree)
}
}
}