Lined Notebook

[6730] [Java] 장애물 경주 난이도

by HeshAlgo

<장애물 경주 난이도>

문제 설명

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWefy5x65PoDFAUh&categoryId=AWefy5x65PoDFAUh&categoryType=CODE

 

내 생각

조건문을 이용해 올라가는 장애물인지 내려가는 장애물인지 판별했습니다. 이 후, abs()를 이용해 차이값 비교를 통해 가장 큰값을 저장시키는 방식으로 구현했습니다.

 

푼 시간

13분 01초

 

작성 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 장애물 경주 난이도
import java.io.FileInputStream;
import java.util.*;
 
public class E6730 {
 
    public static void main(String args[]) throws Exception {
        String path = "input.txt";
        System.setIn(new FileInputStream(path));
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int test_case = 1; test_case <= T; test_case++) {
            int[] answer = new int[2];
            int n = sc.nextInt();
            int[] height = new int[n];
            for (int i = 0; i < n; i++) {
                height[i] = sc.nextInt();
            }
            int upHeight = 0;
            int downHeight = 0;
            
            for (int i = 0; i < height.length-1; i++) {
                if (height[i] < height[i+1]) {
                    upHeight = Math.abs(height[i] - height[i+1]);
                    if (answer[0< upHeight)
                        answer[0= upHeight;
                } else {
                    downHeight = Math.abs(height[i] - height[i+1]);
                    if (answer[1< downHeight)
                        answer[1= downHeight;
                }
            }
            System.out.println("#" + test_case + " " + answer[0+ " " + answer[1]);
            
        }
        
    }
    
}
 
 

 

실행 결과

 

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기