Java/백준
백준 2675번 - 문자열 반복
내이름효주
2024. 1. 9. 14:37
[내가 쓴 코드]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int n = 0; n < a; n++) {
int x = sc.nextInt();
String word = sc.next();
for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < x; j++) {
System.out.print(word.charAt(i));
}
}
System.out.println();
}
sc.close();
}
}
- 처음에 단어를 nextLine으로 하고 반복문을 1로 시작하게 해서 짰더니 틀림
(띄어쓰기가 단어 중간에 있을 수도 있어서 그런듯)
+ [새로 짠 코드]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine()); // 테스트케이스 개수
StringBuilder sb = new StringBuilder(); // string을 합쳐주는 클래스
for (int i = 0; i < a; i++) {
StringTokenizer st = new StringTokenizer(br.readLine()); // 문자열을 우리가 지정한 구분자로 쪼개주는 클래스
int r = Integer.parseInt(st.nextToken()); // 반복 횟수
String s = st.nextToken(); // 문자열 입력 받기
for (int j = 0; j < s.length(); j++) {
for (int k = 1; k <= r; k++) {
sb.append(s.charAt(j)); // StringBuilder에 캐릭터 r번 append시키기
}
}
sb.append("\n");
}
System.out.print(sb);
}
}