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

[๋ฐฑ์ค€ 9076] ์ ์ˆ˜ ์ง‘๊ณ„ (Swift) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 9076] ์ ์ˆ˜ ์ง‘๊ณ„ (Swift)

๋น„๋น„ bibi 2022. 12. 6. 12:38

9076๋ฒˆ: ์ ์ˆ˜ ์ง‘๊ณ„

  • ํ’€์ด ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์œผ๋‚˜, ArraySlice(SubSequence)์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋œ ๋ฌธ์ œ.
    • ArraySlice์— ๋Œ€ํ•œ ๊ธ€์€ ์—ฌ๊ธฐ์— ๋”ฐ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

let count = Int(readLine()!)!
(1...count).forEach { num in
    let inputArr = readLine()!.split(separator: " ").map { Int($0)! }.sorted()
    let newArr = Array<Int>(inputArr[1...3]) // ์ƒˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉ
    if newArr[2] - newArr[0] >= 4 {
        print("KIN")
    } else {
        print(newArr.reduce(0, +))
    }
}
let count = Int(readLine()!)!
(1...count).forEach { num in
    let inputArr = readLine()!.split(separator: " ").map { Int($0)! }.sorted()
    let newArr = inputArr[1...3] // ArraySlice ์‚ฌ์šฉ
    if newArr[3] - newArr[1] >= 4 {
        print("KIN")
    } else {
        print(newArr.reduce(0, +))
    }
}

ํ•˜์ง€๋งŒ ์ƒ๊ฐํ•ด ๋ณด๋‹ˆ.. ์‚ฌ์‹ค sorted()๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค..!

let count = Int(readLine()!)!
(1...count).forEach { num in
    let inputArr = readLine()!.split(separator: " ").map { Int($0)! }.sorted()
    if inputArr[3] - inputArr[1] >= 4 {
        print("KIN")
    } else {
        print(inputArr[1...3].reduce(0, +))
    }
}