haju__log

[python][SWEA][D2] 1940. 가랏! RC카! 본문

SWEA

[python][SWEA][D2] 1940. 가랏! RC카!

haju 2023. 5. 21. 11:32

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&problemLevel=3&contestProbId=AV5PjMgaALgDFAUq&categoryId=AV5PjMgaALgDFAUq&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=PYTHON&select-1=3&pageSize=10&pageIndex=4 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

✅ 문제 풀이

  • 코드는 쉬우나 문제의 조건 이해하기가 넘 어려웠다. 😂
  • c[0]==0 일 때, speed 유지하고 거리에다가 speed 더해주면 된다.
  • c[0]==1 일 때, speed에 c[1]만큼 더해주고, 거리에다가 speed 더해주면 된다.
  • c[0]==2 일 때, 감속 이므로, 조건에 만약 c[1]이 기존 speed 보다 크다면 0으로 초기화
  • 아니라면 c[1]만큼 speed에서 빼주고, 거리에다가 speed 더해주기

✅ 입력 조건 이해하기

 

 

T=int(input())
for t in range(1,T+1):
    n=int(input())
    dis=0
    speed=0

    for i in range(n):
        c=list(map(int,input().split()))
        #0일 경우에는 속도를 유지한다.
        if c[0]==0:
            dis += speed
        #가속
        elif c[0]==1:
            speed+=c[1]
            dis+=speed
        #감속
        elif c[0]==2:
            if speed<c[1]:
                speed=0
            else:
                speed-=c[1]
                dis+=speed
    print("#%d %d" %(t,dis))