CodingTEST

[SW Expert D2] 1940. 가랏! RC카!

경걍 2023. 11. 18. 03:42
반응형

1940. 가랏! RC카!

 

SW Expert Academy

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

swexpertacademy.com

 

 

문제 분석

 

  • 입력으로 매 초마다 아래와 같은 command 가 정수로 주어짐
    • 0 : 현재 속도 유지.
    • 1 : 가속
    • 2 : 감속
  • 입력으로 주어진 N 개의 command 를 모두 수행했을 때, N 초 동안 이동한 거리를 출력해라
  • RC 카의 초기 속도는 0

 

해결 포인트

 

  • 가속일 경우 주어진 speed만큼 증가
  • 감속일 경우 주어진 speed만큼 감소 (0이하면 0으로)

 

코드

 

import java.util.Scanner;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        //System.setIn(new FileInputStream("res/input.txt"));
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();

        for(int test_case = 1; test_case <= T; test_case++) {
            int N = sc.nextInt();

            int speed = 0;
            int result = 0;
            for (int i = 0; i < N; i++) {
                int command = sc.nextInt();
                switch (command) {
                    case 1:
                        speed += sc.nextInt();
                        break;
                    case 2:
                        speed -= sc.nextInt();
                        break;
                    default:
                        break;
                }
                if(speed < 0)
                    speed = 0;
                
                result += speed;
            }


            System.out.printf("#%d %d\n", test_case, result);
        }
    }
}
반응형