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 |