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

[๋ฐฑ์ค€ 14582] ์˜ค๋Š˜๋„ ์กŒ๋‹ค (Swift) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 14582] ์˜ค๋Š˜๋„ ์กŒ๋‹ค (Swift)

๋น„๋น„ bibi 2023. 2. 1. 12:16

14582๋ฒˆ: ์˜ค๋Š˜๋„ ์กŒ๋‹ค

์•ผ๊ตฌ ๊ทœ์น™์„ ์ž˜ ๋ชฐ๋ผ์„œ ์‹œํ–‰์ฐฉ์˜ค๊ฐ€ ์žˆ์—ˆ๋‹ค๐Ÿฅฒ

์ด ๋ฌธ์ œ์—์„œ ์ฃผ์˜๊นŠ๊ฒŒ ์ฝ์–ด์•ผ ํ•  ํฌ์ธํŠธ๋Š”

  1. ์ฒซ ๋ฒˆ์งธ ์ค„ ์ž…๋ ฅ์€ ์šธ๋ฆผ ์ œ๋ฏธ๋‹ˆ์Šค์˜ 1ํšŒ ์ดˆ, 2ํšŒ ์ดˆ, โ€ฆ, 9ํšŒ ์ดˆ ๋“์ ์ด ์ฃผ์–ด์ง„๋‹ค (๋ชจ๋‘ ์ดˆ ๋“์ ). ๋‘ ๋ฒˆ์งธ ์ค„ ์ž…๋ ฅ์€ ์Šคํƒ€ํŠธ๋งํฌ ๊ฑธ๋ฆฌ๋ฒ„์Šค์˜ 1ํšŒ ๋ง, 2ํšŒ ๋ง, โ€ฆ, 9ํšŒ ๋ง ๋“์ ์ด ์ฃผ์–ด์ง„๋‹ค (๋ชจ๋‘ ๋ง ๋“์ )
  2. ์Šคํƒ€ํŠธ๋งํฌ ๊ฑธ๋ฆฌ๋ฒ„์Šค์˜ ์ด ๋“์ ์ด ์šธ๋ฆผ ์ œ๋ฏธ๋‹ˆ์Šค์˜ ์ด ๋“์ ๋ณด๋‹ค ๋งŽ๋‹ค. ์ฆ‰ ์ด๋ฏธ ์ง„ ๊ฒฝ๊ธฐ์ธ๋ฐ ์—ญ์ „ํŒจ๋ฅผ ๋‹นํ–ˆ๋Š”์ง€ ๊ทธ๋ƒฅ ์ฐธํŒจ๋ฅผ ๋‹นํ–ˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋‹ค. ๋”ฐ๋ผ์„œ, ๊ฐ ํŒ€ ์ ์ˆ˜์˜ ์ด ํ•ฉ์„ ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ 1ํšŒ์ดˆ, 1ํšŒ๋ง, 2ํšŒ์ดˆ, 2ํšŒ๋งโ€ฆ ์˜ ๊ฐ ์‹œ์  ์ค‘์—์„œ ํ•œ ๋ฒˆ์ด๋ผ๋„ ์šธ๋ฆผ ์ œ๋ฏธ๋‹ˆ์Šค๊ฐ€ ์ด๊ธฐ๊ณ  ์žˆ๋Š” ์ˆœ๊ฐ„์ด ์žˆ๋Š”์ง€ ( ์ ์ˆ˜๊ฐ€ ๋†’์•˜๋˜ ์ ์ด ์žˆ๋Š”์ง€ ) ๋ฅผ ํŒ๋‹จํ•˜๋ฉด ๋œ๋‹ค.

ํ’€์ด1 (ํ‹€๋ฆผ)

let ullimArr = readLine()!.split(separator: " ").map { Int($0)! }
let startlinkArr = readLine()!.split(separator: " ").map { Int($0)! }
var ullimScore = 0
var startlinkScore = 0
var isUllimWined = false
for index in (0..<9) {
    ullimScore += ullimArr[index] // ์ดˆ
    startlinkScore += startlinkArr[index] // ๋ง
    if ullimScore > startlinkScore { // ํ•œ๋ฒˆ์— ๋”ํ•ด์„œ ๋น„๊ต ( ์ž˜๋ชป๋จ )
        isUllimWined = true
    }
}
if isUllimWined {
    print("Yes")
} else {
    print("No")
}

๋‚ด๊ฐ€ ๊ณ„์† ์™œ๋งžํ‹€(์™œ ๋งž๋Š”๋ฐ ํ‹€๋ฆฌ๋‹ค๊ณ  ํ•˜์ง€?)์„ ๋ฐ˜๋ณตํ–ˆ๋˜ ์ฝ”๋“œ์ด๋‹คใ… 

๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ ์šธ๋ฆผ์˜ ์ ์ˆ˜์™€ ์Šคํƒ€ํŠธ๋งํฌ์˜ ์ ์ˆ˜๋ฅผ ๋”ํ•œ ๋‹ค์Œ, ์šธ๋ฆผ์ด ์Šคํƒ€ํŠธ๋งํฌ๋ณด๋‹ค ์ ์ˆ˜๊ฐ€ ๋†’๋‹ค๋ฉด isUllimWined ํ”Œ๋ž˜๊ทธ๋ฅผ true๋กœ ๋ฐ”๊พผ๋‹ค.

ํ•˜์ง€๋งŒ ์ด ํ’€์ด๋ฐฉ์‹์€ 1ํšŒ์ดˆ, 1ํšŒ๋ง, 2ํšŒ์ดˆ, 2ํšŒ๋ง โ€ฆ ๊ฐ๊ฐ ๋น„๊ตํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ 1ํšŒ(์ดˆ+๋ง), 2ํšŒ(์ดˆ+๋ง), โ€ฆ ๋กœ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— โ€œ์šธ๋ฆผ์ด nํšŒ ์ดˆ์—์„œ๋งŒ ์ด๊ธฐ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐโ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค!

(์ถœ์ฒ˜ : ๋ฐฑ์ค€ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ )

์•„๋ž˜ ๋ฐ˜๋ก€๋ฅผ ๋ณด์ž.

๋ฐ˜๋ก€

0 0 3 0 0 0 0 0 0
0 2 1 5 0 0 0 0 0
//your answer:No
//correct answer:Yes

์œ„ ๋ฐ˜๋ก€์™€ ๊ฐ™์ด 3ํšŒ ์ ์ˆ˜์—์„œ 3ํšŒ ์ดˆ์—๋Š” ์šธ๋ฆผ์ด ์ด๊ธด ์ ์ด ์žˆ๋‹ค. (3:2)

ํ•˜์ง€๋งŒ ์œ„์˜ ํ‹€๋ฆฐ ํ’€์ด์™€ ๊ฐ™์ด, 3ํšŒ ์ „์ฒด์˜ ์ ์ˆ˜๋กœ ๊ณ„์‚ฐํ•˜๋ฉด ์šธ๋ฆผ์ด ์ด๊ธด ์ ์ด ์—†๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค. (4:2)

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์šธ๋ฆผ์˜ ์ ์ˆ˜๋งŒ ์žˆ๋Š” nํšŒ ์ดˆ์˜ ์ƒํƒœ๋„ ๋น„๊ตํ•ด์•ผ ํ•œ๋‹ค.

ํ’€์ด2 (๋งž์Œ)

let ullimArr = readLine()!.split(separator: " ").map { Int($0)! }
let startlinkArr = readLine()!.split(separator: " ").map { Int($0)! }
var ullimScore = 0
var startlinkScore = 0
var isUllimWined = false
for index in (0..<9) {
    ullimScore += ullimArr[index]
    if ullimScore > startlinkScore {
        isUllimWined = true
    }
    startlinkScore += startlinkArr[index]
}
isUllimWined ? print("Yes") : print("No")