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

[๋ฐฑ์ค€ 2576] ํ™€์ˆ˜ (Swift) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 2576] ํ™€์ˆ˜ (Swift)

๋น„๋น„ bibi 2022. 9. 17. 10:39

2576๋ฒˆ: ํ™€์ˆ˜

  • ํ•ฉ์€ reduce, ์ตœ์†Ÿ๊ฐ’์€ min์œผ๋กœ ๊ตฌํ–ˆ๋‹ค
  • ํ™€์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š” (-1 ์ถœ๋ ฅ)

ํ’€์ด

var oddArr: [Int] = []
(1...7).forEach { _ in
    let input = Int(readLine()!)!
    if input % 2 == 1 {
        oddArr.append(input)
    }
}
if oddArr.isEmpty {
    print(-1)
} else {
    print(oddArr.reduce(0, +))
    print(oddArr.min()!)
}