Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[SW Expert Academy] 1208. flatten 본문

SW_Expert

[SW Expert Academy] 1208. flatten

코드와이 2021. 2. 2. 23:59

문제 링크

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

 

SW Expert Academy

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

swexpertacademy.com

 

 

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);
	}
	
}