728x90
https://www.acmicpc.net/problem/2675
2675번: 문자열 반복
문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다
www.acmicpc.net
코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t != 0) {
int r = sc.nextInt();
String s = sc.next();
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < r; j++)
System.out.print(s.charAt(i));
}
System.out.println();
t--;
}
sc.close();
}
}
설명
sc.next();를 이용해 문자열을 공백 기준으로 입력받는다. (nextLine()은 개행 기준이므로 오류)
System.out.print();로 개행 없이 출력한다.
728x90
'JAVA > BOJ' 카테고리의 다른 글
[백준] 1157번: 단어 공부 (1) | 2021.07.19 |
---|---|
[백준] 1929번: 소수 구하기 (1) | 2021.07.18 |
[백준] 4153번: 직각삼각형 (0) | 2021.07.18 |
[백준] 1546번: 평균 (0) | 2021.07.11 |
[백준] 2753번: 윤년 (1) | 2021.07.11 |