코드와이
[SW Expert Academy] 1208. flatten 본문
문제 링크
swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV139KOaABgCFAYh
package D3;
import java.util.Scanner;
public class flatten {
static int max = 0;
static int min = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
for(int tc = 1 ; tc <= 10 ; tc++) {
sb.append("#" + tc + " ");
int dump = sc.nextInt();
int[] arr = new int[100];
int max_idx = 0;
int min_idx = 0;
for(int i = 0; i < 100 ; i++) {
int tmp = sc.nextInt();
arr[i] = tmp;
if(tmp > arr[max_idx]) max_idx = i;
if(tmp < arr[min_idx]) min_idx = i;
}
System.out.print(sb);
flat(arr, dump, max_idx, min_idx);
sb.setLength(0);
}
}
public static void findMaxMin(int[] arr) {
for(int i = 0 ; i < arr.length; i++) {
if ( arr[max] < arr[i]) max = i;
if ( arr[min] > arr[i]) min = i;
}
}
public static void flat(int[] arr, int d, int max_idx, int min_idx) {
if (d == 0 || arr[max_idx] - arr[min_idx] <= 1) {
System.out.println(arr[max_idx] - arr[min_idx]);
return;
}
arr[max_idx] -= 1;
arr[min_idx] += 1;
findMaxMin(arr);
flat(arr, --d, max, min);
}
}
'SW_Expert' 카테고리의 다른 글
[SW Expert Academy] 6853. 직사각형과 점 (0) | 2021.02.04 |
---|---|
[SW Expert Academy] 5432. 쇠막대기 자르기 (0) | 2021.02.03 |
[SW Expert Academy] 6808. 규영이와 인영이의 카드게임 (0) | 2021.02.03 |
[SW Expert Academy] 2817. 부분 수열의 합 (0) | 2021.02.03 |
[SW Expert Academy] 9280. 진용이네 주차 타워 (0) | 2021.02.03 |