티스토리 뷰

728x90

문제

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

 

SW Expert Academy

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

swexpertacademy.com

 

 

풀이 및 소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		for(int tc=1;tc<11;tc++) {
			int n = Integer.parseInt(br.readLine());
			sb.append("#").append(tc).append(" ");
			int[] box = new int[100];
			st = new StringTokenizer(br.readLine());
			for(int i=0;i<100;i++) {
				box[i] = Integer.parseInt(st.nextToken());
			}
			int ans = 0;
			Arrays.sort(box);
			if(box[0]==box[99]||box[0]+1==box[99]) {
				sb.append("0");
				break;
			}
			for(int i=1;i<=n;i++) {
				Arrays.sort(box);
				box[99]-=1;
				box[0]+=1;
				
				if(box[0]==box[99]||box[0]+1==box[99]) {
					break;
				}
			}
			Arrays.sort(box);
			ans = box[99]-box[0];
			sb.append(ans+"\n");
		}
		System.out.println(sb);
	}

}
반응형