๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Coding Test

[Level 2] ์นดํŽซ (Python)

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์นดํŽซ

Leo๋Š” ์นดํŽซ์„ ์‚ฌ๋Ÿฌ ๊ฐ”๋‹ค๊ฐ€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ค‘์•™์—๋Š” ๋…ธ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ณ  ํ…Œ๋‘๋ฆฌ 1์ค„์€ ๊ฐˆ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋Š” ๊ฒฉ์ž ๋ชจ์–‘ ์นดํŽซ์„ ๋ดค์Šต๋‹ˆ๋‹ค. Leo๋Š” ์ง‘์œผ๋กœ ๋Œ์•„์™€์„œ ์•„๊นŒ ๋ณธ ์นดํŽซ์˜ ๋…ธ๋ž€์ƒ‰๊ณผ ๏ฟฝ๏ฟฝ

programmers.co.kr

 

import math

def solution(brown, yellow):
    total = brown + yellow
    
    m, n = [], []
    
    for i in range(1, math.floor(math.sqrt(total)) + 1):
        if divmod(total, i)[1] == 0:
            n.append(i)
            m.append(int(total/i))
    
    for i in range(len(n)):
        if 2*(m[i] + n[i]) - 4 == brown:
            return(m[i], n[i])

 

โ–ท ๊ฐˆ์ƒ‰ ๊ฒฉ์ž์˜ ์ˆ˜(Brown), ๋…ธ๋ž€์ƒ‰ ๊ฒฉ์ž์˜ ์ˆ˜(Yellow), ๊ฐ€๋กœ์˜ ๊ธธ์ด(m), ์„ธ๋กœ์˜ ๊ธธ์ด(n)์— ๋Œ€ํ•œ ๊ด€๊ณ„์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

(1) Brown + Yellow = m × n

(2) Brown = 2 × (m + n) - 4

 

โ–ท (1)์„ ๋งŒ์กฑํ•˜๋Š” ์ฃผ์–ด์ง„ ๊ฐˆ์ƒ‰ ๊ฒฉ์ž์˜ ์ˆ˜์™€ ๋…ธ๋ž€์ƒ‰ ๊ฒฉ์ž์˜ ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š”๋‹ค.

 

โ–ท (1)์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘ (2)๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด ์ด๋ฅผ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

โ–ถ []๋Š” list๋ฅผ ํ• ๋‹นํ•œ๋‹ค.

 

โ–ถ divmod ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋‚˜๋ˆ„๋Š” ๊ฐ’, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋‚˜๋ˆŒ ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๊ฐ€ ํฌํ•จ๋œ ํŠœํ”Œ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

โ–ถmath ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์˜ฌ๋ฆผ์ด ๊ฐ€๋Šฅํ•œ ceil ํ•จ์ˆ˜์™€ ๋‚ด๋ฆผ์ด ๊ฐ€๋Šฅํ•œ floor ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

'Programming > Coding Test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Level 2] H-Index (Python)  (0) 2020.10.05
[Level 2] ์œ„์žฅ (Python)  (0) 2020.09.30
[Level 2] ๊ด„ํ˜ธ ๋ณ€ํ™˜ (Python)  (0) 2020.09.30
[Level 2] ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด (Python)  (0) 2020.09.29
[Level 2] ๋‹ค์Œ ํฐ ์ˆซ์ž (Python)  (0) 2020.09.24