Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 2798] ๋ธ”๋ž™์žญ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 2798] ๋ธ”๋ž™์žญ (Swift)

๋น„๋น„ bibi 2022. 9. 21. 14:51

2798๋ฒˆ: ๋ธ”๋ž™์žญ

  • ํ•ฉ์ด ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉด์„œ ์ตœ๋Œ€ํ•œ ๊ฐ€๊นŒ์šด ์ˆซ์ž 3๊ฐœ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•ด ๊ทธ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ.

์ ‘๊ทผ

  • ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•˜๋‹ˆ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ๊ณจ๋ผ์„œ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค.
    • ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ตœ๋Œ“๊ฐ’๋ถ€ํ„ฐ ๋”ํ–ˆ์„ ๋•Œ ์ •๋‹ต์ด ์•„๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    • ์˜ˆ์ œ1์ธ ํ•ฉ์ด 21์ด ๋˜๋Š” ์„ธ ์ˆซ์ž๋„, [5, 6, 7, 8, 9] ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉฐ [6, 7, 8] ์„ ๋”ํ•œ 21์ด์—ˆ๋‹ค.
    • ์—ฌ๊ธฐ์„œ ๊ทธ๋ฆฌ๋””๋กœ ์ ‘๊ทผํ•˜๋ฉด ์•ˆ๋˜๊ฒ ๋‹ค๋Š” ๊ฑธ ์•Œ์•„์ฐจ๋ ธ์–ด์•ผ ํ–ˆ๋‹ค..!
  • 1์‹œ๊ฐ„์ •๋„ ์‚ฝ์งˆ์„ ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๊ตฌ๊ธ€๋ง์˜ ๋„์›€์„ ๋ฐ›์•˜๊ณ , ๊ทธ๋ฆฌ๋””๊ฐ€ ์•„๋‹Œ ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์“ฐ๋Š” ๋ฌธ์ œ์˜€๋‹ค.
    • ์ฆ‰ ์ฃผ์–ด์ง„ ์นด๋“œ์—์„œ 3๊ฐœ์˜ ์กฐํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•˜๊ณ , ๊ทธ ์ค‘์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์กฐํ•ฉ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
    • ์‹œ๊ฐ„์ œํ•œ์ด 1์ดˆ๋ผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ? ํ–ˆ๋Š”๋ฐ, 3์žฅ์˜ ์กฐํ•ฉ๋งŒ์„ ์ฐพ๋Š”๊ฑฐ๋ผ ๊ทธ๋Ÿฐ์ง€ ๊ฐ€๋Šฅํ–ˆ๋‹ค..โ€™0โ€™
    • ๊ทธ๋Ÿฌ๊ณ  ๋ณด๋‹ˆ ์ „์— ์กฐํ•ฉ์„ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋˜ ์ผ๊ณฑ๋‚œ์Ÿ์ด ์™€ ๋น„์Šทํ•˜๋‹ค. ์•Œ๊ณ  ๋‚˜์„œ์•ผ ๋ณด์ด๋Š”๊ตฌ๋‚˜.

ํ’€์ด

  • ์นด๋“œ 3์žฅ์˜ ๋ชจ๋“  ์กฐํ•ฉ์„ ์ฐพ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 3์ค‘ for๋ฌธ์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•ด์„œ๋„ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž„!
  • currentMax โ‰ค sum && sum โ‰ค totalMax ๋Œ€์‹  Range.contains(sum) ๋ฅผ ์‚ฌ์šฉํ•ด ๋ดค๋Š”๋ฐ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.
let input = readLine()!.split(separator: " ").map { Int($0)! }
let totalMax = input[1]
let count = input[0]
var cards = readLine()!.split(separator: " ").map { Int($0)! }
var currentMax = 0
for i in 0..<count {
    for j in i + 1..<count { // ์ง์ „ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์‚ฌ์šฉํ•œ ๋‹ค์Œ ์š”์†Œ๋ถ€ํ„ฐ ์‚ฌ์šฉ - ์ค‘๋ณต ํ”ผํ•˜๊ธฐ
        for k in j + 1..<count {
            print(cards[i], cards[j], cards[k])
            let sum = cards[i] + cards[j] + cards[k]
            if (currentMax...totalMax).contains(sum) {
                currentMax = sum
            }
        }
    }
}
print(currentMax)