✅ 여학생의 경우 음수로 변환 후 저장하여 구분
: 배열 하나로 관리가 가능
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
const (
on = "1"
off = "0"
)
func main() {
defer wr.Flush()
switchs, students := parse()
for _, v := range students {
switch {
case v > 0:
maleAction(switchs, v)
case v < 0:
v *= -1
femaleAction(switchs, v)
}
}
result := make([]string, len(switchs))
for i, v := range switchs {
switch v {
case true:
result[i] = on
default:
result[i] = off
}
}
for i := 0; i < len(switchs); i += 20 {
last := i + 20
if last > len(switchs) {
last = len(switchs)
}
wr.WriteString(strings.Join(result[i:last], " "))
wr.WriteRune('\n')
}
}
func maleAction(switchs []bool, index int) {
for i := index - 1; i < len(switchs); i += index {
switchs[i] = !switchs[i]
}
}
func femaleAction(switchs []bool, index int) {
var i, j int
index--
switchs[index] = !switchs[index]
i = index + 1
j = index - 1
for i < len(switchs) && j >= 0 {
if switchs[i] != switchs[j] {
break
}
switchs[i] = !switchs[i]
switchs[j] = !switchs[j]
i++
j--
}
}
func parse() (switchs []bool, students []int) {
swN := readInt()
switchs = make([]bool, swN)
for i := 0; i < swN; i++ {
n := readInt()
if n == 1 {
switchs[i] = true
}
}
stN := readInt()
students = make([]int, stN)
for i := 0; i < stN; i++ {
n := readInt()
m := readInt()
students[i] = m
if n == 2 {
students[i] *= -1
}
}
return
}
// input
var (
sc *bufio.Scanner
wr *bufio.Writer
)
func init() {
sc = bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
wr = bufio.NewWriter(os.Stdout)
}
func readInt() int {
sc.Scan()
n, _ := strconv.Atoi(sc.Text())
return n
}