[Python] ์ด๋ถ„ ํƒ์ƒ‰
ยท
Algorithm/Python
์ด๋ถ„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ def binary_search(a, x): start = 0 end = len(a) - 1 while start a[mid]: start = mid + 1 else: end = mid - 1 return -1 d = [1, 4, 9, 16, 25, 36, 49, 64, 81] print(binary_search(d, 36)) print(binary_search(d, 50)) #1 ์ค‘๊ฐ„ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค. #2 ์ฐพ๋Š” ๊ฐ’๊ณผ ์ค‘๊ฐ„ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค. #3 ๊ฐ™๋‹ค๋ฉด ์œ„์น˜๋ฒˆํ˜ธ๋ฅผ ๋Œ๋ ค์ฃผ๊ณ  #4 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์˜ค๋ฅธ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ) #5 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์™ผ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ)
[Python] ๋ฐฑ์ค€ 2420 - ์‚ฌํŒŒ๋ฆฌ์›”๋“œ
ยท
Algorithm
๋ฌธ์ œ ์‚ฌํŒŒ๋ฆฌ์›”๋“œ๋Š” ์ธํ„ฐ๋„ท์œผ๋กœ๋งŒ ์กด์žฌํ•˜๋Š” ๋ฏธ์Šคํ…Œ๋ฆฌํ•œ ๋‚˜๋ผ์ด๋‹ค. ์‚ฌํŒŒ๋ฆฌ์›”๋“œ์—๋Š” 2๊ฐœ์˜ ์„œ๋ธŒ๋„๋ฉ”์ธ์ด seunghwan.royal.gov.sw์™€ kyuhyun.royal.gov.sw ์ด ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์ด couple.royal.gov.sw์œผ๋กœ ํ•ฉ์ณ์งˆ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋„๋ฉ”์ธ ๊ด€๋ฆฌ ์„ผํ„ฐ SWNIC(์„ผํ„ฐ์žฅ: ๊น€๋™๊ทœ)์—๋Š” ์—„๊ฒฉํ•œ ๋ฃฐ์ด ์žˆ๋‹ค. ๋‘ ์„œ๋ธŒ๋„๋ฉ”์ธ์„ ํ•ฉ์น  ๋•Œ, ์œ ๋ช…๋„์˜ ์ฐจ์ด๊ฐ€ ๋„ˆ๋ฌด ์ฐจ์ด๋‚˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ ๋‘ ์„œ๋ธŒ๋„๋ฉ”์ธ์„ ๊ฒฐํ˜ผ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. ์„œ๋ธŒ๋„๋ฉ”์ธ์˜ ์œ ๋ช…๋„๋Š” ์ •์ˆ˜์ด๋‹ค. ๋‘ ์„œ๋ธŒ๋„๋ฉ”์ธ์˜ ์œ ๋ช…๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ์ฐจ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ๋‘ ๋„๋ฉ”์ธ์˜ ์œ ๋ช…๋„ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (-2,000,000,000 ≤ N, M ≤ 2,000,000,000) ์ถœ๋ ฅ ์ฒซ์งธ ์ค„์— ๋‘ ์œ ๋ช…๋„์˜ ์ฐจ์ด (..
[Python] ๋ฐฑ์ค€ 1427 - ์†ŒํŠธ์ธ์‚ฌ์ด๋“œ
ยท
Algorithm
๋ฌธ์ œ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค. ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๊ทธ ์ˆ˜์˜ ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ณด์ž. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ •๋ ฌํ•˜๊ณ ์žํ•˜๋Š” ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์ถœ๋ ฅ ์ฒซ์งธ ์ค„์— ์ž๋ฆฌ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ •๋‹ต n = int(input()) a = list(str(n)) a.sort() a.reverse() for n in a: print(n, end="") n์„ ์ž…๋ ฅ๋ฐ›๊ณ  a๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ €์žฅํ•œ๋‹ค. sort()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌํ•˜๊ณ , reverse()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋’ค์ง‘๋Š”๋‹ค. for๋ฌธ์„ ์ด์šฉํ•ด ์ถœ๋ ฅํ•œ๋‹ค.
[Python] ์„ ํƒ์ •๋ ฌ
ยท
Algorithm/Python
์„ ํƒ์ •๋ ฌ ๋ฐ์ดํ„ฐ์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„ ๊ทธ ๊ฐ’๊ณผ ๋ฒ”์œ„์˜ ๋งจ ์•ž์— ์žˆ๋Š” ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์„ ํƒ์ •๋ ฌ ๊ณผ์ • ์˜ˆ๋ฅผ ๋“ค์–ด [2, 4, 5, 1, 3] ๊ฐ€ ์žˆ์„ ๋•Œ, ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ์ฒซ๋ฒˆ์งธ ๊ฐ’๊ณผ ๊ทธ ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค. (์ด๋ฏธ ์ •๋ ฌ ์™„๋ฃŒ๋œ ๊ฐ’์€ | ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.) [2, 4, 5, 1, 3] # ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 1 ์ฒซ๋ฒˆ์งธ ๊ฐ’ : 2 [1 | 4, 5, 2, 3] # ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 2 ์ฒซ๋ฒˆ์งธ ๊ฐ’ : 4 [1, 2 | 5, 4, 3] # ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 3 ์ฒซ๋ฒˆ์งธ ๊ฐ’ : 5 [1, 2, 3 | 4, 5] # ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 4 ์ฒซ๋ฒˆ์งธ ๊ฐ’ : 4 //๋ณ€ํ™”์—†์Œ [1, 2, 3, 4 | 5] # ์ž๋ฃŒ๊ฐ€ 5 ํ•˜๋‚˜๋งŒ ๋‚จ์Œ. ์ข…๋ฃŒ ๊ฒฐ๊ณผ [1, 2, 3, 4, 5] ์„ ํƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ def sel_..