-
Notifications
You must be signed in to change notification settings - Fork 27
/
443.go
55 lines (50 loc) · 1000 Bytes
/
443.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
// UVa 443 - Humble Numbers
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
in, _ := os.Open("443.in")
defer in.Close()
out, _ := os.Create("443.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
lst := make([]int, n+1)
lst[1] = 1
current, p2, p3, p5, p7 := 1, 1, 1, 1, 1
for current < n {
for lst[p2]*2 <= lst[current] {
p2++
}
for lst[p3]*3 <= lst[current] {
p3++
}
for lst[p5]*5 <= lst[current] {
p5++
}
for lst[p7]*7 <= lst[current] {
p7++
}
current++
lst[current] = min(lst[p2]*2, min(lst[p3]*3, min(lst[p5]*5, lst[p7]*7)))
}
var order string
switch {
case n != 11 && n%10 == 1:
order = strconv.Itoa(n) + "st"
case n != 12 && n%10 == 2:
order = strconv.Itoa(n) + "nd"
case n != 13 && n%10 == 3:
order = strconv.Itoa(n) + "rd"
default:
order = strconv.Itoa(n) + "th"
}
fmt.Fprintf(out, "The %s humble number is %d.\n", order, lst[current])
}
}