-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
사이 사이 띄어쓰기가 어디서 나오는걸까요..?
21.10.10 23:25 작성 조회수 153
0
```
```
답변을 작성해보세요.
0

비비
21.10.12 03:53
안녕하세요! 결과 출력 하실 때
for (char c : T.solution(str).toCharArray()) {
System.out.print(c + " ");
}
이 부분에서 띄어쓰기 " " 넣으셔서 띄어쓰기 출력됩니다.
0

australialove19
질문자21.10.10 23:25
package main;
import java.util.Scanner;
public class Main {
public String solution(String str) {
String answer = "";
str = str + " ";
int cnt = 1;
for (int i = 0; i < str.length() - 1; i++) {
if(str.charAt(i) == str.charAt(i+1)) cnt++;
else {
answer += str.charAt(i);
if(cnt > 1) {
answer += cnt;
cnt = 1;
}
}
}
return answer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
Main T = new Main();
for (char c : T.solution(str).toCharArray()) {
System.out.print(c + " ");
}
}
}
답변 2