본문 바로가기

Java/SWEA

[JAVA] SWEA 1946. 간단한 압축 풀기

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PmkDKAOMDFAUq&categoryId=AV5PmkDKAOMDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=JAVA&select-1=2&pageSize=10&pageIndex=2

 

SW Expert Academy

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

swexpertacademy.com

 

 

import java.util.*;

class Solution {    
    
    public static void main(String args[]) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt(); 
        
        for (int t = 1; t <= T; t++)
        { 
            HashMap<String,Integer> map = new HashMap<>();
            ArrayList<String> list = new ArrayList<>();
            
            int N = sc.nextInt();
            for(int i=0; i<N; i++)
            {
                String s = sc.next();
                int num = sc.nextInt();
                
                map.put(s, num);
            }
            
            Iterator<String> keys = map.keySet().iterator();
            while(keys.hasNext()) {
                String key = keys.next();
                for(int j=0; j<map.get(key); j++)
                {
                    list.add(key);
                }
            }
            
            // 열의 길이가 10인 2차원 배열 생성
            int rows = (int) Math.ceil((double)list.size()/10);
            String arr[][] = new String[rows][10];
            
            for(int i=0; i<rows; i++)
            {
                for(int j=0; j<10; j++)
                {
                    if(i*10+j < list.size())
                        arr[i][j] = list.get(i*10+j);
                }
            }
            
            System.out.printf("#%d\n",t);
            for(int i=0; i<rows; i++)
            {
                for(int j=0; j<10; j++)
                {
                    if(arr[i][j] != null)
                        System.out.print(arr[i][j]);
                }
                System.out.println();
            }
            
        }

    }
}

 

 

왜,,,어째서

 

 

 

정답 

 

 

import java.util.*;

class Solution {    
    
    public static void main(String args[]) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt(); 
        
        for (int t = 1; t <= T; t++)
        { 
            StringBuilder answer = new StringBuilder();
            int N = sc.nextInt();
            for(int i=0; i<N; i++)
            {
                String s = sc.next();
                int num = sc.nextInt();
               
                for(int j=0; j<num; j++)
                {
                	answer.append(s);
                }
            }
            
            
            System.out.printf("#%d\n",t);
            for(int i=0; i<answer.length(); i++)
            {
            	System.out.print(answer.charAt(i));
            	if(i%10==9)
            		System.out.println();
            }
            System.out.println();
        }
    }
}

'Java > SWEA' 카테고리의 다른 글

[JAVA] SWEA 1940. 가랏! RC카!  (0) 2024.05.08
[JAVA] SWEA 1945. 간단한 소인수분해  (0) 2024.05.08
[JAVA] SWEA 1948. 날짜 계산기  (0) 2024.05.06
[JAVA] SWEA 1959. 두개의 숫자열  (0) 2024.05.05
[JAVA] SWEA 1961. 숫자 배열 회전  (0) 2024.05.05