0315 TIL

looggiยท2023๋…„ 3์›” 15์ผ
2

TILs

๋ชฉ๋ก ๋ณด๊ธฐ
36/114
post-thumbnail

๋นฝ์ค€ ๋ฌธ์ œํ’€๊ธฐ

โžก๏ธ ์„คํƒ•๋ฐฐ๋‹ฌ

def solution(n):
    if n%5 ==0:
        return n//5
    elif n%5 in (1,3):
        return n//5+1
    elif n%5 in (2,4):
        if n in (4,7):
            return -1
        return n//5+2

N=int(input())
print(solution(N))

3,5๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์—†๋Š” ์ˆ˜๋Š” 4,7 ๋ฟ์ด๊ณ 
๊ตฌ์„ฑํ•  ๋•Œ ์ตœ์†Œ ๋ด‰์ง€ ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 5๊ฐ€ ์ตœ๋Œ€๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฒŒ ์ข‹๋‹ค
5๋‹จ์œ„๋กœ ๊ทœ์น™์ด ์žˆ์–ด์„œ
kg์ด 5๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์งˆ ๋•Œ๋Š” ๋ชซ์„,
๋‚˜๋จธ์ง€๊ฐ€ 1,3 ์ผ๋•Œ๋Š” ๋ชซ+1
๋‚˜๋จธ์ง€๊ฐ€ 2,4 ์ผ๋•Œ๋Š” ๋ชซ+2๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค

๊ตฌ์„ฑํ•˜๋Š” ์ˆ˜๋ผ๋ฆฌ์˜ ์ฐจ์ด๊ฐ€ 2๋ผ์„œ ๊ฐ€๋Šฅํ–ˆ๋˜ ํ’€์ด์ธ ๊ฒƒ ๊ฐ™๋‹ค

โžก๏ธ ์„คํƒ•๋ฐฐ๋‹ฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ

cnt = 0

while n > 0:
    if n % 5 == 0:
        cnt += n // 5
        break
    
    n -= 3
    cnt += 1
    
    if n < 0:
        cnt = -1
print(cnt)

์ด๊ฒŒ ์ •์„ ํ’€์ด์ธ ๊ฒƒ ๊ฐ™๋‹ค
n์ด 0๋ณด๋‹ค ํด ๋•Œ n์ด 5์˜ ๋ฐฐ์ˆ˜๋ผ๋ฉด cnt์— ๋ชซ๋งŒํผ ๋”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ณ 
์•„๋‹ˆ๋ผ๋ฉด n์—์„œ 3์„ ๋นผ๊ณ  3์„ ๋บ์œผ๋‹ˆ๊นŒ cnt+=1
๋งŒ์•ฝ 3์„ ๋บ€ n์˜ ๊ฐ’์ด 0๋ณด๋‹ค ์ž‘๋‹ค๋ฉด 4๋‚˜ 7์— ํ•ด๋‹น๋˜๋ฏ€๋กœ cnt์— -1์„ ๋„ฃ์–ด์ฃผ๊ณ  n<0 ์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์—์„œ ํƒˆ์ถœํ•˜๊ฒŒ ๋œ๋‹ค

์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์ด ๋ฐ˜๋ณต๋ผ์„œ ๋ฐ–์—์„œ -1์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™๋‹ค

โœ”๏ธ ์ •๋ฆฌ

n=int(input())
cnt = 0

while n >= 0:
    if n % 5 == 0:
        cnt += n // 5
        print(cnt)
        break
        
    n -= 3
    cnt += 1
    
else: print(-1)

while๋ฌธ ์กฐ๊ฑด์— 0์ด ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉด else์ดํ•˜๊ฐ€ ๋ฆฌํ„ด๊ฐ’์ด ๋˜์–ด ํ‹€๋ฆฐ๋‹ค
else๊ฐ€ ์—†์œผ๋ฉด while์—์„œ cnt๊ฐ€ ํ”„๋ฆฐํŠธ ๋˜๊ณ  ๋‚œ ํ›„์—-1์ด ๋˜ ํ”„๋ฆฐํŠธ๋œ๋‹ค

โœ”๏ธ ํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”๋ด„

def solution(n):
    cnt = 0
    while n >= 0:
        if n % 5 == 0:
            cnt += n // 5
            return cnt
        n -= 3
        cnt += 1
    return -1

n=int(input())
print(solution(n))

โžก๏ธ 1,2,3 ๋”ํ•˜๊ธฐ ์žฌ๊ท€๋กœ

def solution(x):
    answer=0
    if x==1:
       return answer+1
    elif x==2:
        return answer+2
    elif x==3:
        return answer+4
    return solution(x-3)+solution(x-2)+solution(x-1)

T=int(input())
for _ in range(T):
    n=int(input())
    print(solution(n))

์—ญ์‹œ ์žฌ๊ท€๋กœ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค
๊ทผ๋ฐ ๋” ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ.. ์กฐ๊ฑด์ด ์ € ์„ธ๊ฐ€์ง€๋ฅผ ๋”ํ•˜๋Š” ๊ฑฐ๋‹ˆ๊นŒ ์•ˆ๋˜๋ ค๋‚˜??

๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ‘ผ ์žฌ๊ท€(์™„์ „ํƒ์ƒ‰)

์žฌ๊ท€: ํ˜„์žฌ ํ•จ์ˆ˜์˜ ์ƒํƒœ๋ฅผ ๊ณ„์† ๋„˜๊ธฐ๊ณ  ํƒˆ์ถœ์กฐ๊ฑด์„ ๋งŒ๋“ค์–ด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค

  • ํƒˆ์ถœ์กฐ๊ฑด
    • 1,2,3์œผ๋กœ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์›ํ•˜๋Š” ๊ฐ’ n๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด ์„ฑ๊ณต
    • ์•„๋‹ˆ๋ผ๋ฉด ์‹คํŒจ/ ์ถ”๊ฐ€ ๊ณ„์‚ฐ
def solution(sum,n):
    if sum>n:
        return 0
    elif sum == n: # ์„ฑ๊ณต
        return 1
    curr =0
    for i in range(1,4):
        curr+=solution(sum+i,n)
    return curr

T=int(input())
for _ in range(T):
    n=int(input())
    print(solution(0,n))

ํ•œ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด
n=4์ผ ๋•Œ
solution(0,4)๋Š” if๋ฌธ์—๋Š” ๊ฑธ๋ฆฌ์ง€ ์•Š๊ณ  curr์ดํ•˜์˜ for๋ฌธ์—๋งŒ ๊ฑธ๋ฆฐ๋‹ค
๊ฒฐ๊ณผ๋กœ ๋ฆฌํ„ด๋˜๋Š” curr์€ solution(1,4)+solution(2,4)+solution(3,4)์ด๋‹ค
solution(1,4)๋Š” ๋‹ค์‹œ if๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ณ  curr์ดํ•˜์—์„œ ๋‹ค์‹œ curr์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค ๊ฐ๊ฐ์— ๋Œ€ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์„ ์จ๋ณด๋ฉด

  • solution(1,4)->solution(2,4)+solution(3,4)+solution(4,4)
  • solution(2,4)->solution(3,4)+solution(4,4)+solution(5,4)
  • solution(3,4)->solution(4,4)+solution(5,4)+solution(6,4)

๊ฐ๊ฐ์ด ๋‹ค์‹œ solutionํ•จ์ˆ˜๋ฅผ ๋Œ ๋•Œ sum>n์ด๋ฉด 0์ด๊ณ  ๊ฐ™์œผ๋ฉด 1์ด๋ฏ€๋กœ ์ž‘์€ ๊ฒƒ(๋ธ”๋ก์ฒ˜๋ฆฌํ•œ ๊ฒƒ)์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค์‹œ ๋ฐ˜๋ณต๋ฌธ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค

  • solution(2,4)->solution(3,4)+solution(4,4)+solution(5,4)
  • solution(3,4)->solution(4,4)+solution(5,4)+solution(6,4)
  • solution(3,4)->solution(4,4)+solution(5,4)+solution(6,4)

๋‹ค์‹œ

  • solution(3,4)->solution(4,4)+solution(5,4)+solution(6,4)

๊ฒฐ๊ณผ์ ์œผ๋กœ sum<n์ด๋ผ๋ฉด ๋ฐ˜๋“œ์‹œ 1๊ฐœ ์ด์ƒ์˜ 1๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ด๊ฒŒ ๋˜๊ณ 
์ •๋ฆฌํ•ด๋ณด๋ฉด sum<n์ด๋ผ๋ฉด sum๊ณผ n์˜ ์ฐจ์ด*1๋งŒํผ ๊ฐ’์„ ๋‚ด๋†“๋Š”๋‹ค

๋ฌดํŠผ ๋”ฐ๋ผ์„œ solution(0,4)์˜ ๋‹ต์€ 7์ด ๋œ๋‹ค

์‹œ๊ฐ„์€ ๋‚ด๊ฐ€ ํ’€์—ˆ๋˜ ์žฌ๊ท€๋ณด๋‹ค๋Š” ๋” ์›์ดˆ์ ์œผ๋กœ ์žฌ๊ท€๋ผ์„œ ๊ทธ๋Ÿฐ์ง€ ๋” ์˜ค๋ž˜ ๊ฑธ๋ ธ๋Š”๋ฐ ๋‚ด๊ฐ€ ์›๋ž˜ ์›ํ•˜๋˜ ๊ฑด ์ด๊ฑฐ์˜€๋‹ค. ์ดˆ๊ธฐ๊ฐ’์„ ํ•˜๋‚˜๋„ ์ฃผ์ง€ ์•Š๊ณ  1์ด ๋ฒ ์ด์Šค๊ฐ€ ๋ผ์„œ ํƒ€๊ฒŸ ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๐Ÿค“ ์ตœ์ดˆ์˜ ๋น„๊ต๊ฐ’์„ ํ•จ์ˆ˜ ์ž์ฒด์— ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ ๊ทธ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค ์ง„์งœ ๋˜‘๋˜‘ํ•œ ์‚ฌ๋žŒ,,

โžก๏ธ ์กฐ๊ฑด์— ๋งž๋Š” ์ €์ž์™€ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐ

SELECT B.BOOK_ID, A.AUTHOR_NAME, DATE_FORMAT(B.PUBLISHED_DATE,"%Y-%m-%d") AS PUBLISHED_DATE 
FROM BOOK B 
JOIN AUTHOR A
ON B.AUTHOR_ID = A.AUTHOR_ID
WHERE B.CATEGORY = "๊ฒฝ์ œ"
ORDER BY B.PUBLISHED_DATE

์ด ๊ฒฝ์šฐ์—๋Š” ์กฐ์ธ์„ ์“ด๋‹ค๋ฉด LEFT JOIN, INNER JOIN ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๊ณ 
์“ฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ…Œ์ด๋ธ” 2๊ฐœ๋ฅผ FROM ์ ˆ์— ๊ทธ๋ƒฅ ๋‚˜์—ดํ•˜๊ณ 

WHERE์ ˆ์—์„œ ์กฐ๊ฑด์„ AND๋กœ ๋ฌถ์–ด์„œ๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค

โžก๏ธ ์กฐ๊ฑด์— ๋งž๋Š” ๋‹ค๋ฅธ ๊ฑฐ

SELECT BOOK_ID, AUTHOR_NAME, DATE_FORMAT(PUBLISHED_DATE, '%Y-%m-%d')
FROM BOOK as b, AUTHOR as a
WHERE b.author_id = a.author_id AND category = "๊ฒฝ์ œ"
ORDER BY PUBLISHED_DATE

AS PUBLISHED_DATE๋Š” ๊ตณ์ด ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์ •๋‹ต์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹น

๊ทธ๋ฆฌ๊ณ  SQL์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œํ’€๊ธฐ

โžก๏ธ ๊ฐœ์ธ์ •๋ณด์ˆ˜์ง‘ ์œ ํšจ๊ธฐ๊ฐ„

def solution(today, terms, privacies):
    terms=dict([x.split(' ') for x in terms])# ์•ฝ๊ด€์ข…๋ฅ˜,์œ ํšจ๊ธฐ๊ฐ„
    # print(terms) # {'A': '6', 'B': '12', 'C': '3'}
    privacies=[x.split(' ') for x in privacies]# ๋‚ ์งœ,์•ฝ๊ด€์ข…๋ฅ˜
    # print(privacies) # [['2021.05.02', 'A'], ['2021.07.01', 'B'], ['2022.02.19', 'C'], ['2022.02.20', 'C']]
    today=[int(x) for x in today.split('.')]# y,m,d
    # print(today) #[2022, 5, 19]
    answer = []
    for i,p in enumerate(privacies):
        date, kinds = p
        y,m,d=map(int,date.split('.'))
        d=d+int(terms[kinds])*28
        if d>28:
            if d%28 != 0:
                m+=d//28
                d=d%28
            else:
                m+=d//28-1
                d=28
        if m>12:
            if m%12 != 0:
                y+=m//12
                m=m%12
            else:
                y+=m//12-1
                m=12

        if y<today[0]:
            answer.append(i+1)
        elif y==today[0] and m<today[1]:
            answer.append(i+1)
        elif y==today[0] and m==today[1] and d<=today[2]:
            answer.append(i+1)
            
    return answer

๋‚ ์งœ ํ˜•ํƒœ๋ฅผ ๊ผญ ์ง€ํ‚ฌ ํ•„์š” ์—†์ด ๋…„, ์›”, ์ผ์„ ๋‹ค ์ผ๋กœ ๊ณ„์‚ฐํ•ด์„œ ๋น„๊ตํ•ด์„œ ๋” ์ž‘์€ ๊ฒฝ์šฐ์— ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ํ›จ์”ฌ ์งง์€ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค ํ˜„ํƒ€ ์”จ๊ฒŒ์˜จ๋‹น ํ•ณ

๋ฌธ์ œ๊ฐ€ ์‰ฌ์šด๋ฐ ๋ฉ˜๋ถ•์ด ์™”๋˜ ๋ถ€๋ถ„์ด if m>12 if d>28 ์ดํ•˜์—์„œ ์ด ๋ณ€์ˆ˜๋“ค์— ๊ฐ’์„ ์žฌ์ •์˜ํ•ด์ค„ ๋•Œ์˜€๋Š”๋ฐ, m์˜ ๊ฐ’์„ ๋จผ์ € ๋ฐ”๊พธ๋ฉด ๋’ค์— m์ด ๋“ค์–ด๊ฐ€๋Š” ์‹์˜ ๊ฐ’๋„ ๋‚ด๊ฐ€ ์˜๋„ํ•œ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค๋Š” ๋‹น์—ฐํ•œ ์‚ฌ์‹ค์„ ๋ญ”๊ฐ€ ์™„์ „ํžˆ ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค ใ…‹ใ…‹ใ…‹ ์ง„์งœ ๊ฐ‘์ž๊ธฐ ์—ฌ๊ธฐ๊ฐ€ ์ง€๊ตฌ๊ฐ€ ์•„๋‹Œ๊ฐ€..? ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค ใ…‹ใ…‹ใ…‹ใ…‹ ํ”„๋ฆฐํŠธ ์ฐ์œผ๋ฉด์„œ ๋„ˆ๋ฌด ๋†€๋žฌ๋‹ค ใ…‹ใ…‹ใ…‹

๋ฌดํŠผ ๊ทธ๋ž˜๋„ ์–ธํŒจํ‚น์ด๋ž‘ enumerate ์š”๊ธดํ•˜๊ฒŒ ์จ์„œ ๊ธฐ๋ถ„ ์ข‹๊ฒŒ ์ž์•ผ๊ฒ ๋‹ค ๐Ÿฅฑ

profile
looooggi

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 3์›” 17์ผ

๊พธ์ค€ํžˆ ํ•˜๋Š” ๋ชจ์Šต ๋ฉ‹์ง‘๋‹ˆ๋‹ค

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ