hye-log

[SWEA]1208번 Flatten(JAVA) 본문

CodingTest/SWEA

[SWEA]1208번 Flatten(JAVA)

iihye_ 2023. 7. 30. 00:59

0. 문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV139KOaABgCFAYh 

 

SW Expert Academy

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

swexpertacademy.com

 

1. 문제 설명

1) 최고점과 최저점의 간격 줄이는 평탄화 진행하기

 

2. 입출력

// input
834
42 68 35 1 70 25 79 59 63 65 6 46 82 28 62 92 96 43 28 37 92 5 3 54 93 83 22 17 19 96 ...

// output
#1 13

 

3. 코드

import java.util.*;

public class swea1208 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = 10; // 테스트 케이스의 개수
		
		for(int t = 1; t <= T; t++) {
			int dump = sc.nextInt(); // 덤프 횟수
			int[] boxes = new int[100]; // 상자들
			for(int i = 0; i < 100; i++) { 
				boxes[i] = sc.nextInt();
			}
			
			for(int d = 0; d < dump; d++) {
				Arrays.sort(boxes); // 정렬
				boxes[0]++; // 최소값 증가
				boxes[99]--; // 최대값 감소
			}
			
			Arrays.sort(boxes); // 정렬
			System.out.printf("#%d %d\n", t, boxes[99]-boxes[0]);
		}
	}
}

 

실행 결과

 

4. 회고

1) 배열 정렬을 통해 최대값과 최소값을 구할 수 있음

 

5. Github

https://github.com/iihye/Algorithm/blob/main/SWEA/swea1208.java

728x90
Comments