1 ๋ถ„ ์†Œ์š”

ํŒŒ์ด์ฌ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ

[๋ฐฑ์ค€-10989: ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ3] ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๋ณด๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋ผ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.
๊ตฌ๊ธ€๋ง ํ•ด๋ณด๋‹ˆ, ๋งŽ์€ ์ž…๋ ฅ์„ ์ „๋ถ€ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ฑฐ๋‚˜, sort() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’๋‹ค๊ณ  ํ•˜๋”๋ผ..

๊ธฐ์กด์ฝ”๋“œ

[๊ธฐ์กด์ฝ”๋“œ]

import sys

N = int(sys.stdin.readline())

arr = []
for i in range(N):
    num = int(sys.stdin.readline())
    arr.append(num)

arr.sort()

for i in range(N):
    print(arr[i])

๊ธฐ์กด์— ๊ณ„์† ํ•˜๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ sys.stdin.readline()์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , ๋ฐฐ์—ด arr์— ์ž…๋ ฅ๋“ค์„ ๋‹ด์•˜๋‹ค.
๊ทธ ๊ฒฐ๊ณผ๋ฅผ sort()๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌ์„ ํ•˜๊ณ  ์ œ์ถœํ–ˆ์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋Š” ํ•ด๊ฒฐ์ด ๋˜์ง€ ์•Š์•˜๋‹ค.

ํ•ด๊ฒฐ

[์ •๋‹ต์ฝ”๋“œ]

# (์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด์—์„œ) ๋ชจ๋“  ์ž…๋ ฅ์„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋ฉด ๋‹น์—ฐํžˆ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ์ž…๋‹ˆ๋‹ค.
# sort()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์ดˆ๊ณผ๋‚  ํ™•๋ฅ  ๋†’์Œ

import sys

N = int(sys.stdin.readline())

arr = [0 for _ in range(10001)]

for i in range(N):
    num = int(sys.stdin.readline())
    arr[num] += 1

for i in range(len(arr)):
    if arr[i] != 0:
        for j in range(arr[i]):
            print(i)

๋จผ์ € ์ ๋‹นํ•˜๊ฒŒ ํฐ ๋ฐฐ์—ด์„ ์ง์ ‘ ์„ ์–ธํ•œ๋‹ค.
๊ทธ ๋ฐฐ์—ด์€ ์ „๋ถ€ 0์œผ๋กœ ์ดˆ๊ฐ€ํ™”!

๊ทธ๋Ÿฌ๋ฉด ๋ฐฐ์—ด์ด ์ ๋‹นํžˆ ํฌ๊ฒŒ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜๊ณ , ์ž…๋ ฅ๋“ค์„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด์˜ ์ž๋ฆฟ์ˆ˜์— ์ž…๋ ฅ ๊ฐ’๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
๋งŒ์•ฝ N=3, input=1,2,3์ด๋ผ๊ณ  ํ•˜๋ฉด arr = [0, 1, 1, 1, 0, 0, 0, โ€ฆ , 0]๊ณผ ๊ฐ™์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ๊ทธ ์ž๋ฆฌ์— +=1๋กœ ๊ณ„์† ๋”ํ•ด์ค€๋‹ค. -> ๋‚˜์ค‘์— ๋ฐ˜๋ณต ํšŸ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž„.

์•„๋ž˜ ๋ฐ˜๋ณต๋ฌธ์—์„œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฐฐ์—ด์˜ ์š”์†Œ ๊ฐ’(1 ์ด์ƒ)๋งŒํผ ๋ฐ˜๋ณต์„ ํ•˜๋ฉฐ, ๊ทธ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ

https://coarmok.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%ACpython-%EB%B0%B1%EC%A4%80-10989%EB%B2%88-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%B4%88%EA%B3%BC
https://wikidocs.net/130182

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ