-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path324.go
49 lines (43 loc) · 755 Bytes
/
324.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 324 - Factorial Frequencies
package main
import (
"fmt"
"io"
"math/big"
"os"
)
func factorial(n int) string {
res := big.NewInt(1)
for ; n > 1; n-- {
res.Mul(res, big.NewInt(int64(n)))
}
return res.String()
}
func output(out io.Writer, str string) {
var res [10]int
for i := range str {
digit := str[i] - '0'
res[digit]++
}
for i, v := range res {
if fmt.Fprintf(out, " (%d)%5d", i, v); i != 4 && i != 9 {
fmt.Fprint(out, " ")
} else {
fmt.Fprintln(out)
}
}
}
func main() {
in, _ := os.Open("324.in")
defer in.Close()
out, _ := os.Create("324.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fprintf(out, "%d! --\n", n)
output(out, factorial(n))
}
}