Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1244๋ฒˆ - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ (๊ตฌํ˜„)

๊ฐ์ž ๐Ÿฅ” 2023. 2. 15. 21:30
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/1244

 

1244๋ฒˆ: ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ

์ฒซ์งธ ์ค„์—๋Š” ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๋Š” 100 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์Šค์œ„์น˜์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ผœ์ ธ ์žˆ์œผ๋ฉด 1, ๊บผ์ ธ์žˆ์œผ๋ฉด 0์ด๋ผ๊ณ  ํ‘œ์‹œํ•˜๊ณ  ์‚ฌ์ด์— ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

๊ตฌํ˜„๋ฌธ์ œ๋ผ์„œ, ๊ทธ๋ƒฅ ๋ณด์ž๋งˆ์ž ํ’€์ด๋ฅผ ๋– ์˜ฌ๋ฆฐ ํ›„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์—ฌ์ž/๋‚จ์ž์ผ๊ฒฝ์šฐ ์Šค์œ„์น˜๋ฅผ ์ผœ๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅธ๋ฐ ์ด ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋งŒ ์ž˜ ํ•ด์ฃผ๋ฉด๋œ๋‹ค. 

์•„,, ๊ทผ๋ฐ ๋ฒ”์œ„์„ค์ •? ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ๋ญ”๊ฐ€ ์ž๊พธ ์ด์ƒํ•ด์„œ ์—„์ฒญ ํ‹€๋ ธ๋‹ค... ์ง„์งœ ๋ฐ”๋ณด... ์„ฑ๋ณ„์ด ๋‚จ์ž์ผ ๋•Œ ๋ถ€๋ถ„์—์„œ ์ฒ˜์Œ์— while๋ฌธ์œผ๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ num+=num ์—ฌ๊ธฐ๋ถ€๋ถ„์—์„œ, num์ด 3์ผ๋•Œ, 3, 6, 9,,, ์ด๋ ‡๊ฒŒ ์ปค์ ธ์•ผํ•˜๋Š”๋ฐ ๋‚ด ์ฝ”๋“œ์ผ๊ฒฝ์šฐ num๋˜ํ•œ ๊ณ„์† ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— num์€ 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ 3+3์ด ๋˜์—ˆ๋‹ค๊ฐ€ 6+6 ์ด ๋˜์—ˆ๋‹ค๊ฐ€... ๋ง๋„์•ˆ๋˜๊ฒŒ ์ปค์ง„๋‹ค. ์ด๋ถ€๋ถ„์ด ์ž˜๋ชป๋œ๊ฒƒ์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋ช‡๋ฒˆ์„ ํ‹€๋ฆฌ๊ณ  ์ง€์ง€๊ณ  ๋ณถ์•˜๋ƒ??

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์ง„์งœ ;;; ๋„ˆ๋ฌด ํ™”๊ฐ€๋‚ฌ๋‹ค... ๋งž์•˜๋Š”๋ฐ  ๋„๋Œ€์ฒด ์™œ๋งž์•˜์ง€? ์‹ถ์–ด์„œ ๋ฐ”๋ณด๊ฐ™์ด ์ด๊ฒƒ์ €๊ฒƒ ๋˜ ๋ฐ”๊ฟ”๋ณด๋ฉด์„œ ํ‹€๋ฆฐ ๋ถ€๋ถ„์„ ์ฐพ์„๋•Œ๊นŒ์ง€ ๊ณ„์† ์ˆ˜์ •์— ์ˆ˜์ •์„ ๋ฐ˜๋ณต ํ•ด๋ดค๋‹ค. ๊ฒฐ๊ตญ.. ์ €๋†ˆ์ด ๋ฌธ์ œ์˜€๋˜๊ฒƒ...

์ดˆ๊ธฐ์˜ num์„ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ์ €์žฅํ•ด๋‘๊ณ  ๊ณ„์† ๋”ํ•ด์คฌ์–ด์•ผํ•˜๋Š”๋ฐ.. ์ผ๋‹จ ์–ด์จ‹๋“  ๋‚œ for๋ฌธ์˜ stride๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค..ํ›„..

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

import Foundation

let switchNum = Int(readLine()!)!
var switches = [-1] + readLine()!.split(separator: " ").map { Int($0)! }
let studentNum = Int(readLine()!)!
var students = [[Int]]()
for _ in 0..<studentNum {
    students.append(readLine()!.split(separator: " ").map{ Int($0)! })
}

for student in students {
    switch student[0] {
    case 1:
        let num = student[1]
        for i in stride(from: num, through: switchNum, by: num) {
            switches[i] = switches[i] == 1 ? 0 : 1
        }
    default:
        let num = student[1]
        switches[num] = switches[num] == 1 ? 0 : 1
        for i in 1...switchNum/2 {
            if num - i < 1 || num + i > switchNum {
                break
            }
            if switches[num - i] == switches[num + i] {
                switches[num - i] = switches[num - i] == 1 ? 0 : 1
                switches[num + i] = switches[num + i] == 1 ? 0 : 1
            } else {
                break
            }
        }
    }
}

for i in 1...switchNum {
    print(switches[i], terminator: " ")
    if i % 20 == 0 {
        print()
    }
}

 

ํž˜๋“ค๋‹ค ๋ฒŒ์จ... ์ด๋ฒˆ์ฃผ ์™œ์ด๋Ÿฌ์ง€ ใ… ใ…  ํ™•์‹คํžˆ ๊ทธ์ €๊ป˜๋ถ€ํ„ฐ ์ •์‹ ์ด ๋‚˜๊ฐ€์žˆ์—ˆ๋Š”๋ฐ ์•„์ง๋„ ์ •์‹ ์ด ๋Œ์•„์˜ค์ง€ ์•Š์€ ๊ธฐ๋ถ„์ด๋‹ค. ์ด์ƒํ•ด.. ์ด์ƒํ•œ๊ฑฐ์—์„œ ์ž๊พธ ํ‹€๋ฆฌ๊ณ  ๋จธ๋ฆฌ๋„ ์•ˆ๋Œ์•„๊ฐ€๊ณ  ์ง‘์ค‘๋„ ์•ˆ๋˜๊ณ ใ… ใ…  ์‹ค๋ฒ„4 ํ‘ธ๋Š”๋ฐ ์‹œ๊ฐ„์„ ์–ผ๋งˆ๋‚˜ ์“ด๊ฑฐ์•ผ ํ•˜...

๋ฐ˜์‘ํ˜•