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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2309๋ฒˆ - ์ผ๊ณฑ๋‚œ์Ÿ์ด

๊ฐ์ž ๐Ÿฅ” 2022. 12. 28. 14:39
๋ฐ˜์‘ํ˜•

๐Ÿ”– TIL - for ๋ฌธ ์•ž์— ์ด๋ฆ„๋ถ™์—ฌ์„œ ๋‹ค์ค‘ํฌ๋ฌธ ํƒˆ์ถœํ•˜๊ธฐ!

์˜ˆ์ „์— ํ’€๋ฉด์„œ๋„ ๊ณต๋ถ€ํ–ˆ์—ˆ๋˜ ๊ฒƒ์ธ๋ฐ, for ๋ฃจํ”„์— ์ด๋ฆ„์„ ๋ถ™์—ฌ์ค€ ํ›„, ๋‹ค์ค‘ ํฌ๋ฌธ์—์„œ 'ํŠน์ • ํฌ๋ฌธ'์„ ํƒˆ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

for i in 0..<9 {
    for j in 0..<9 {
        let temp = i * j

        if temp == 50 {
            print("๋‚˜๊ฐˆ๊ฑฐ์•ผ")
            break
        }
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด์ž. i์™€ j ์˜ ๊ณฑ์ด 50์ด ๋˜๋ฉด , ์ด์ค‘ํฌ๋ฌธ์„ ๋ชจ๋‘!! ํƒˆ์ถœํ•˜๊ณ  ์‹ถ๋‹ค. ๊ทธ๋Ÿผ,,, ์–ด๋””์— break๋ฅผ ๊ฑธ์–ด์ค˜์•ผ ๋ฌด์‚ฌํžˆ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ง๋“ฌ ์œ„ ์ฝ”๋“œ์—์„œ๋Š” break ๊ฑธ๋ ค์žˆ๋Š” ์œ„์น˜๋ฅผ ๋ณด๋ฉด, ๋‚ด๋ถ€์— ์žˆ๋Š” for๋ฌธ๋งŒ ํƒˆ์ถœํ•˜๊ณ  ์™ธ๋ถ€ for๋ฌธ์€ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์™ธ๋ถ€ for๋ฌธ์„ ํƒˆ์ถœํ•˜๋ ค๋ฉด?

outer_for_loop: for i in 0..<9 {
    for j in 0..<9 {
        let temp = i * j

        if temp == 50 {
            print("๋‚˜๊ฐˆ๊ฑฐ์•ผ")
            break outer_for_loop
        }
    }
}

์ด๋ ‡๊ฒŒ outer_for_loop ์ด๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค. breakํ•ด์ค„๋•Œ, ์ด๋ ‡๊ฒŒ ๊ฑธ์–ด์ฃผ๋ฉด, ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์™ธ๋ถ€ for๋ฌธ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ”๋กœ ํƒˆ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค! ๊ธฐ์–ตํ•ด๋†“์ž ~ 

 

๐ŸŸ  ๋ฌธ์ œ

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

 

2309๋ฒˆ: ์ผ๊ณฑ ๋‚œ์Ÿ์ด

์•„ํ™‰ ๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๋‚œ์Ÿ์ด๋“ค์˜ ํ‚ค๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ํ‚ค๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋ฉฐ, ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด๋ฉฐ, ๊ฐ€๋Šฅํ•œ ์ •๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

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

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฐ”๋กœ ๊ฐ์„ ์žก์•˜๋‹ค. ์ด๋Ÿฐ๋ฌธ์ œ๋„ ์™„์ „ํƒ์ƒ‰์ธ๊ฐ€..? (์™„ํƒ์œผ๋กœ ๋ถ„๋ฅ˜๊ฐ€ ๋˜์–ด์žˆ๋‹ค...)ํ 
9๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๊ณ  7๊ฐœ์˜ ํ•ฉ์ด 100์ด๋ผ๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด, 9๊ฐœ์˜ ์ˆ˜๋ฅผ ์ „์ฒด ๋‹ค ๋”ํ•ด์ฃผ๊ณ , 100์„ ๋นผ๋ฉด, ์–ด๊ธ‹๋‚˜๋Š” ๋‘๋†ˆ์„ ๊ตฌํ•  ์ˆ˜ ๊ฐ€ ์žˆ๊ฒ ์ง€!

i ์™€ j ๋‘ ๊ฐœ๋กœ ๋Œ์•„๊ฐ€๋Š” ์ด์ค‘ for loop๋ฅผ ํ†ตํ•ด "์ „์ฒดํ•ฉ - 100 - i - j"์ด ๋˜๋Š” i ์™€ j๋ฅผ ์ฐพ์•„์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  i ์™€ j๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต !

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

private func solution() {
    var nanjaeng = [Int]()
    var totalSum = 0
    for _ in 0..<9 {
        let temp = Int(readLine()!)!
        nanjaeng.append(temp)
        totalSum += temp
    }

    nanjaeng.sort()
    loop: for i in 0..<9 {
        for j in i+1..<9 {
            if totalSum - nanjaeng[i] - nanjaeng[j] - 100 == 0 {
                for k in 0..<9 {
                    if k != i, k != j {
                        print(nanjaeng[k])
                    }
                }
                break loop
            }
        }
    }
}

solution()

 

์•„๋‹ˆ, ์ด๋ฌธ์ œ ๋‘๋ฒˆ์งธ ํ’€์–ด๋ณด๋Š”๊ฑด๋ฐ!! ์ง€๋‚œ๋ฒˆ์— ํ’€์—ˆ๋˜ ํ’€์ด๋ž‘ ๋˜‘๊ฐ™๋‹ค ใ…‹ใ…‹ใ…‹ ์ข€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ๊ธธ ๋ฐ”๋žฌ๋Š”๋ฐ,, ์•„์ง์€ ์˜ˆ์ „์˜ ๋‚˜ = ์š”์ฆ˜์˜ ๋‚˜์ธ๊ฑด๊ฐ€.. ์‹ถ์–ด์„œ ๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋’ค์ ธ๋ดค๋‹ค. ๊ทผ๋ฐ ,,,, ์ด ๋ฌธ์ œ ๋„ˆ๋ฌด ์‰ฌ์›Œ์„œ ๊ทธ๋Ÿฐ๊ฑฐ๊ฐ™๋‹ค! ๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค๋„ ๋‹ค ๋น„์Šทํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค ใ…Žใ…Ž

๋ฐ˜์‘ํ˜•