ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€ 2309] ์ผ๊ณฑ ๋‚œ์Ÿ์ด (Python)

์€์ง„ 2021. 8. 3. 13:52

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[๋ฐฑ์ค€ 2309] ์ผ๊ณฑ ๋‚œ์Ÿ์ด (Python)
๋ฐฑ์ค€


โœ๏ธIdea Sketch

2021-07-29

1. ๋ธŒ๋ฃจํŠธ ํฌ์Šค(brute force) = ๋ฌด์‹ํ•œ ํž˜ = ์™„์ „ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

2. i๋Š” 0๋ถ€ํ„ฐ 8๋ฒˆ์งธ๊นŒ์ง€ ํƒ์ƒ‰, j๋Š” i+1๋ถ€ํ„ฐ 9๋ฒˆ์งธ๊นŒ์ง€ ํƒ์ƒ‰

  for i in range(8):
    for j in range(i+1, 9):

3. ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒํ•˜๊ธฐ


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-07-29

๋ฐฉ๋ฒ• 1. ํ•จ์ˆ˜ ์„ ์–ธ ํ›„ return, 80ms ํ†ต๊ณผ

height = sorted([int(input()) for _ in range(9)])
total = sum(height)

def solution():
  for i in range(8):
    for j in range(i+1, 9):
      if total - height[i] - height[j] == 100:
        return i, j

i, j = solution()
for k in range(9):
  if k == i or k == j:
    continue
  print(height[k])


๋ฐฉ๋ฒ• 2. sys.exit(0) ์‚ฌ์šฉ, 76ms ํ†ต๊ณผ

import sys

height = sorted([int(input()) for _ in range(9)])
total = sum(height)

for i in range(8):
  for j in range(i+1, 9):
    if total - height[i] - height[j] == 100:
      for k in range(9):
        if k == i or k == j:
          continue
        print(height[k])
      sys.exit(0)