-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path402.go
63 lines (57 loc) · 1.05 KB
/
402.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
// UVa 402 - M*A*S*H
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
var cards [20]int
func count(n, x int) []string {
members := make([]int, n)
for i := range members {
members[i] = i + 1
}
eliminated := 0
here:
for idx := 0; ; idx++ {
memberCount := 0
card := cards[idx]
for i, member := range members {
if member != 0 {
memberCount++
if memberCount%card == 0 {
members[i] = 0
eliminated++
if eliminated == n-x {
break here
}
}
}
}
}
var remaining []string
for _, member := range members {
if member != 0 {
remaining = append(remaining, strconv.Itoa(member))
}
}
return remaining
}
func main() {
in, _ := os.Open("402.in")
defer in.Close()
out, _ := os.Create("402.out")
defer out.Close()
var n, x int
for kase := 1; ; kase++ {
if _, err := fmt.Fscanf(in, "%d%d", &n, &x); err != nil {
break
}
for i := range cards {
fmt.Fscanf(in, "%d", &cards[i])
}
fmt.Fprintf(out, "Selection #%d\n", kase)
fmt.Fprintf(out, "%s\n\n", strings.Join(count(n, x), " "))
}
}