[๋ฐฑ์ค€/python] 11651๋ฒˆ : ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ 2

2021. 3. 11. 23:35

๋ฌธ์ œ

2์ฐจ์› ํ‰๋ฉด ์œ„์˜ ์  N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ขŒํ‘œ๋ฅผ y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ, y์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ ์„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

N = int(input())
dots = []
for i in range(N):
    x, y = map(int, input().split())
    dots.append((x, y))

dots.sort(key=lambda x: (x[1], x[0]))

for i in range(N):
    print(dots[i][0], dots[i][1])

๋žŒ๋‹คํ•จ์ˆ˜์™€ sort()๋ฅผ ์ด์šฉํ•˜์—ฌ dots๋ฅผ ์ •๋ ฌํ•œ๋‹ค.

๋žŒ๋‹คํ•จ์ˆ˜์˜ ์ž์„ธํ•œ ๋‚ด์šฉ๊ณผ sort()์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

 

2021.03.12 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ๋žŒ๋‹คํ•จ์ˆ˜ (lambda)

 

[ํŒŒ์ด์ฌ/python] ๋žŒ๋‹คํ•จ์ˆ˜ (lambda)

๋žŒ๋‹คํ•จ์ˆ˜(lambda)๋Š” ๋Ÿฐํƒ€์ž„์— ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ต๋ช… ํ•จ์ˆ˜๋กœ, ์“ฐ๊ณ  ๋ฒ„๋ฆฌ๋Š” ์ผ์‹œ์ ์ธ ํ•จ์ˆ˜์ด๋‹ค. ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ์„ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์ •์˜ํ•ด๋‘๊ณ  ์“ฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•„์š”ํ•œ ๊ณณ์—์„œ ์ฆ‰์‹œ ์‚ฌ์šฉ

ye333.tistory.com

2021.03.17 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ์ •๋ ฌ - sort(), sorted()

 

[ํŒŒ์ด์ฌ/python] ์ •๋ ฌ - sort(), sorted()

ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํžˆ sort ๋˜๋Š” sorted ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. sort() ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ๋ฐ”๊พธ์–ด ๊ธฐ์กด์˜ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. sorted() ๋ฆฌ์ŠคํŠธ๋ฅผ

ye333.tistory.com

 

728x90

BELATED ARTICLES

more