Problem Solving/BOJ & Programmers

[BOJ] Bronze ๋ฌธ์ œ ํ’€์ด ๋ชจ์Œ (1)

geum 2024. 10. 17. 15:55

2355๋ฒˆ: ์‹œ๊ทธ๋งˆ

๐Ÿ’กํ’€์ด

๋”ฑ ๋ด๋„ for๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์€ ๊ทน์•…๋ฌด๋„ํ•œ ์ž…๋ ฅ ๋ฒ”์œ„๋‹ค. '๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํ•ฉ' ๊ณต์‹์„ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ ์ด ๋•Œ A, B ์ค‘ ์–ด๋Š ์ˆ˜๊ฐ€ ํฐ ์ˆ˜์ธ์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— min/max๋กœ ๋Œ€์†Œ ๊ด€๊ณ„๋งŒ ํ™•์‹คํ•˜๊ฒŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


2530๋ฒˆ: ์ธ๊ณต์ง€๋Šฅ ์‹œ๊ณ„

๐Ÿ’กํ’€์ด

ํ•ญ์ƒ ์‹œ๊ฐ„ ๊ณ„์‚ฐ ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚˜๋ฉด ์Šต๊ด€์ ์œผ๋กœ if๋ฌธ์„ ๋– ์˜ฌ๋ฆฌ๋Š”๋ฐ ์กฐ๊ฑด์„ ์ž˜ ๋‚˜๋ˆ„๊ณ  ์กฐ๊ฑด๋ณ„๋กœ ํ™•์‹คํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ •๋‹ต์ด ๋˜๊ธด ํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‚˜์ฒ˜๋Ÿผ ํ•ญ์ƒ ๋ญ”๊ฐ€ ๋นผ๋จน๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ณ  if๋ฌธ์œผ๋กœ ์ง€์ €๋ถ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋กœ ๋ชจ๋‘ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ดœ์ฐฎ์•„๋ณด์ธ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํฌํ•จํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹œ๊ฐ„ ๊ณ„์‚ฐ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ ์•ž์„œ ๋งํ•œ ๋ฐฉ์‹์ด ๊ฐ€์žฅ ๊น”๋”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ด ์•ž์œผ๋กœ๋Š” ๊ณ„์† ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฌธ์ œ๋ฅผ ํ’€ ์˜ˆ์ •์ด๋‹ค.

 

A, B, C = map(int, input().split())
D = int(input())

# ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ์ดˆ๋กœ ๋ณ€ํ™˜
total_seconds = A * 3600 + B * 60 + C + D

hour = (total_seconds // 3600) % 24  # 24์‹œ๊ฐ„ ์ด์ƒ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌ
minute = (total_seconds % 3600) // 60
sec = total_seconds % 60

print(hour, minute, sec)

2846๋ฒˆ: ์˜ค๋ฅด๋ง‰๊ธธ

๐Ÿ’กํ’€์ด

๋ฌธ์ œ ์ดํ•ด๋Š” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง„ ์•Š์•˜์ง€๋งŒ ํ’€๋ฆด ๋“ฏ ๋ง ๋“ฏ ์•ˆ ํ’€๋ ค์„œ ์ฑ—์ง€ํ”ผํ‹ฐ์˜ ๋„์›€์„ ๋ฐ›์•˜๋‹ค. ๋‚˜๋Š” ์˜ค๋ฅด๋ง‰๊ธธ์˜ ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ๊ธฐ๋กํ•ด๋‘๊ณ  ๋‘ ์ง€์ ์˜ ์ฐจ์ด๋กœ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š” ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ๊ธฐ๋กํ•˜๋Š” ๋ถ€๋ถ„ ๊ตฌํ˜„์— ์‹คํŒจํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ๋ฅผ ๋ชป ํ’€์—ˆ๋‹ค. ๋‚ด๊ฐ€ ์‹œ๋„ํ•œ ๋ฐฉ๋ฒ•๊ณผ ์ฑ—์ง€ํ”ผํ‹ฐ ๋ฐฉ๋ฒ•์˜ ๊ณตํ†ต์ ์€ ํ˜„์žฌ ์˜ค๋ฅด๋ง‰๊ธธ์˜ ์‹œ์ž‘์ ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋‹ค ์ •๋„?

 

์ฑ—์ง€ํ”ผํ‹ฐ ๋ฐฉ์‹์—์„œ ๊ธฐ์–ตํ•ด๋‘ฌ์•ผ๊ฒ ๋‹ค ์‹ถ์—ˆ๋˜ ๋ถ€๋ถ„์€ โ‘  ์˜ค๋ฅด๋ง‰๊ธธ์ด ์ด์–ด์ง€๋Š” ๊ฒฝ์šฐ ๋‘ ์ง€์ ์˜ ์ฐจ์ด๋ฅผ ๊ณ„์† ๋”ํ•ด์ฃผ๋ฉด์„œ ํฌ๊ธฐ๋ฅผ ๊ฐฑ์‹ ํ–ˆ๋‹ค๋Š” ๊ฒƒ๊ณผ โ‘ก ์˜ค๋ฅด๋ง‰๊ธธ์ด ๋Š๊ธฐ์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

N = int(input())

heights = list(map(int, input().split()))

max_uphill = 0

# ํ˜„์žฌ ์˜ค๋ฅด๋ง‰๊ธธ์˜ ์‹œ์ž‘์ ๊ณผ ํฌ๊ธฐ ์ถ”์ ์šฉ ๋ณ€์ˆ˜
current_uphill = 0

for i in range(1, N):
    if heights[i] > heights[i-1]:   # ์˜ค๋ฅด๋ง‰๊ธธ์ด ์ด์–ด์ง€๋Š” ๊ฒฝ์šฐ
        current_uphill += heights[i]-heights[i-1]
    else:
        max_uphill = max(current_uphill, max_uphill)
        
        current_uphill = 0

# ๋งˆ์ง€๋ง‰ ์˜ค๋ฅด๋ง‰๊ธธ์ด ๋ฆฌ์ŠคํŠธ ๋์—์„œ ๋๋‚˜๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
# ์˜ˆ) 1 2 3 4 5 / ๋ฆฌ์ŠคํŠธ๋Š” ๋๋‚ฌ์ง€๋งŒ ์˜ค๋ฅด๋ง‰๊ธธ์ด ๋๋‚˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— else๋ฌธ์— ๋“ค์–ด๊ฐ€์ง€ ๋ชปํ•จ
max_uphill = max(max_uphill, current_uphill)

print(max_uphill)

6322๋ฒˆ: ์ง๊ฐ ์‚ผ๊ฐํ˜•์˜ ๋‘ ๋ณ€

๐Ÿ’กํ’€์ด

์ด ๋ฌธ์ œ๋Š” ์ œ์ถœํ•˜๋‹ˆ๊นŒ 'ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค'๋„ ์•„๋‹ˆ๊ณ  ์ž๊พธ ValueError๊ฐ€ ๋– ์„œ ๋ญ์ง€ ์‹ถ์—ˆ๋‹ค. ์•Œ๊ณ  ๋ณด๋‹ˆ math.sqrt ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ๋•Œ ์Œ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒŒ ๋‹น์—ฐํ•œ ๊ฑด๋ฐ ์ œ๊ณฑํ–ˆ์œผ๋‹ˆ๊นŒ ๋ญ~ ํ•˜๋ฉด์„œ ์ € ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ ์•ˆ ์จ์„œ ๊ทธ๋Ÿฐ ๊ฑฐ์˜€๋‹ค โญ ์ด ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ํ™•์‹คํ•˜๊ฒŒ ํ•˜๋‚˜ ๋ฐฐ์šฐ๊ฒŒ ๋๋‹ค.


19532๋ฒˆ: ์ˆ˜ํ•™์€ ๋น„๋Œ€๋ฉด๊ฐ•์˜์ž…๋‹ˆ๋‹ค

๐Ÿ’กํ’€์ด

์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ผ์ฐจ๋ฐฉ์ •์‹ ํ‘ธ๋Š” ๋ฐฉ๋ฒ• ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ–ˆ๋”๋‹ˆ ํ†ต๊ณผ๋Š” ํ–ˆ๋Š”๋ฐ ์ฝ”๋“œ๊ฐ€ ๊ธธ๊ธฐ๋„ ํ•˜๊ณ  ๋ญ”๊ฐ€ ์ง€์ €๋ถ„ํ•ด์„œ ์ด๊ฒŒ ์ตœ์„ ์ธ๊ฐ€?  ์‹ถ์–ด ์ฑ—์ง€ํ”ผํ‹ฐ์—๊ฒŒ ๋ฌผ์–ด๋ดค๋‹ค๊ฐ€ ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•ด์„œ ๋†€๋ž„ ๋…ธ ์ž. ์ž…๋ ฅ ๋ฒ”์œ„๊ฐ€ ์•ˆ ํฐ๋ฐ ๋‚œ ์™œ ์ €๋ ‡๊ฒŒ ํ•  ์ƒ๊ฐ์„ ๋ชป ํ–ˆ์ง€ ์‹ถ์–ด์„œ ์—ฌ๊ธฐ ๋ฐฑ์—…ํ•ด๋‘”๋‹ค.

 

1) ์ •๋‹ต ์ฝ”๋“œ by ChatGPT

a, b, c, d, e, f = map(int, input().split())

# ๋‘ ๋ณ€์ˆ˜ x์™€ y๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ฐ˜๋ณต๋ฌธ
for x in range(-999, 1000):
    for y in range(-999, 1000):
        # ๋‘ ๋ฐฉ์ •์‹์„ ๋งŒ์กฑํ•˜๋Š” x, y ์ฐพ๊ธฐ
        if a * x + b * y == c and d * x + e * y == f:
            print(x, y)
            break

 

2) ์ •๋‹ต ์ฝ”๋“œ by ๋‚˜

def find_lcm(n1, n2):
    multiply = n1*n2
    
    while n2 != 0:
        n1, n2 = n2, n1%n2
        
    return multiply//n1

a, b, c, d, e, f = map(int, input().split())

x_lcm = find_lcm(abs(a), abs(d))

if a == 0 or d == 0:
    if a == 0:
        y = c//b
        x = (f-e*y)//d
    elif d == 0:
        y = f//e
        x = (c-b*y)//a
elif b == 0 or e == 0:
    if b == 0:
        x = c//a
        y = (f-d*x)//e
    elif e == 0:
        x = f//d
        y = (c-a*x)//b
else:    
    new_a, new_b, new_c = a*(x_lcm//a), b*(x_lcm//a), c*(x_lcm//a)
    new_d, new_e, new_f = d*(x_lcm//d), e*(x_lcm//d), f*(x_lcm//d)

    if (new_a > 0 and new_d > 0) or (new_a < 0 and new_d < 0):
        y = (new_c-new_f)//(new_b-new_e)
    else:
        y = (new_c+new_f)//(new_b+new_e)

    x = (new_c-new_b*y)//new_a

print(x, y)