Jun's Development Journey

[SWEA] 1208. [S/W 문제해결 기본] Array1 - Flatten 본문

SWEA/Intermediate

[SWEA] 1208. [S/W 문제해결 기본] Array1 - Flatten

J_Jayce 2019. 8. 1. 11:26

문제

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

 

SW Expert Academy

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

www.swexpertacademy.com

 

 

풀이

 

이 문제는 의외로 예외처리 해줘야 할 부분이 얼마 없었던 문제이지만 반복문의 첨자를 잘못 쓰는 바람에 본의 아니게 오래 걸렸던 문제다. 앞으로 반복문의 첨자 사용할 때 i 와 j를 잘 구분해야한다는 교훈을 얻은 문제이다...!

 

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
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
public class Solution {
    static Scanner scan = new Scanner(System.in);
    static int[] heights = new int[100];
    
    static int[] find_idxs() {
        int[] idxs = new int[2];
        int Max = 0, Min = 101
        int idx1=0, idx2=0
        for(int i=0;i<heights.length;i++) {
            if(Max<heights[i]) {
                Max = heights[i];
                idx1 = i;
            }
            if(Min > heights[i]) {
                Min = heights[i];
                idx2 = i;
            }
        }
        idxs[0= idx1;
        idxs[1= idx2;
        return idxs;
    }
    
    static void solution() {
        for(int i=1;i<=10;i++) {
            int dump = scan.nextInt();
            int[] idxs;
            for(int j=0;j<100;j++
                heights[j] = scan.nextInt();
            
            for(int j=0;j<dump;j++) {
                idxs = find_idxs();
                if(heights[idxs[0]]-heights[idxs[1]]>1) {
                    heights[idxs[0]]--;
                    heights[idxs[1]]++;
                }
                else 
                    break;
            }
            idxs = find_idxs();
            System.out.println("#"+i+" "+(heights[idxs[0]]-heights[idxs[1]]));
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        solution();
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter