๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 2309 - ์ผ๊ณฑ ๋‚œ์Ÿ์ด

by YWTechIT 2021. 5. 26.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 2309 - ์ผ๊ณฑ ๋‚œ์Ÿ์ด

๋ฐฑ์ค€ 2309 - ์ผ๊ณฑ ๋‚œ์Ÿ์ด


โšก๏ธ ๋‚˜์˜ ํ’€์ด

๋ธŒ๋ฃจํŠธ ํฌ์Šค(brute force)์œ ํ˜• ๋ฌธ์ œ์ธ๋ฐ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ์ค‘ ์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ํฌ๊ธฐ์˜ ํ•ฉ์€ 100์ด ๋œ๋‹ค. ์ฆ‰, sum(arr) - (๋‚œ์Ÿ์ด1 + ๋‚œ์Ÿ์ด2) == 100๊ณผ ๊ฐ™์€ ๋ง์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ช‡ ๋ฒˆ ๋‚œ์Ÿ์ด๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„์•ผํ•œ๋‹ค.

 

  1. sum(arr) - (arr[i] + arr[j]) == 100์ด ๋˜๋ฉด ํ•ด๋‹น ๋‚œ์Ÿ์ด๋ฅผ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด๋‘๊ณ  ๋งˆ์ง€๋ง‰์— arr์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
  2. sum(arr) - (arr[i] + arr[j]) == 100์ด ๋˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํ•œ๋ฒˆ ๋” ์„ ์–ธํ•˜๊ณ  ํ•ด๋‹น ๋‚œ์Ÿ์ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ ํ›„ exit()๋กœ ๋น ์ ธ๋‚˜์˜จ๋‹ค. break๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ๋ฐ˜๋ณต๋ฌธ๋งŒ ๋น ์ ธ๋‚˜์˜ค๊ณ  ๋‹ค์‹œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•˜์ž.

 

# 1๋ฒˆ์งธ ๋ฐฉ๋ฒ• (temp, remove)

n = 9
temp1, temp2 = 0, 0
arr = [int(input()) for _ in range(n)]

for i in range(n):
    for j in range(i+1, n):
        if sum(arr) - (arr[i] + arr[j]) == 100:
            temp1 = arr[i]
            temp2 = arr[j]

arr.remove(temp1)
arr.remove(temp2)

print('\n'.join(map(str, sorted(arr))))
# 2๋ฒˆ์งธ ๋ฐฉ๋ฒ• (continue, exit)

n = 9
arr = [int(input()) for _ in range(n)]
arr.sort()

for i in range(n):
    for j in range(i+1, n):
        if sum(arr) - (arr[i] + arr[j]) == 100:
            for k in range(9):
                if i == k or j == k:
                    continue
                print(arr[k])
            exit()

print('\n'.join(map(str, arr)))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€