-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path400.go
65 lines (59 loc) · 1.09 KB
/
400.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
// UVa 400 - Unix ls
package main
import (
"fmt"
"io"
"os"
"sort"
)
const max = 60
func output(out io.Writer, files []string, cols, maxLen int) {
for i := 0; i < max; i++ {
fmt.Fprint(out, "-")
}
fmt.Fprintln(out)
rows := len(files) / cols
if len(files)%rows != 0 {
rows++
}
format := fmt.Sprintf("%%-%ds", maxLen+2)
for i := 0; i < rows; i++ {
for j := 0; j < cols-1; j++ {
idx := i + j*rows
if idx < len(files) {
fmt.Fprintf(out, format, files[idx])
}
}
idx := i + (cols-1)*rows
if idx < len(files) {
fmt.Fprintln(out, files[idx])
}
}
if rows*cols != len(files) {
fmt.Fprintln(out)
}
}
func main() {
in, _ := os.Open("400.in")
defer in.Close()
out, _ := os.Create("400.out")
defer out.Close()
var n int
for {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
files := make([]string, n)
maxLen := 0
for i := range files {
fmt.Fscanf(in, "%s", &files[i])
length := len(files[i])
if length > maxLen {
maxLen = length
}
}
cols := (max + 2) / (maxLen + 2)
sort.Strings(files)
output(out, files, cols, maxLen)
}
}