728x90
반응형
https://www.acmicpc.net/problem/7120
7120번: String
It sometimes happens that a button on the computer keyboard sticks and then in the printed text there are more than one identical letters. For example, the word "piano" can change into "ppppppiaanooooo". Your task is to write a program that corrects these
www.acmicpc.net
풀이
체크할 문자 char를 선언합니다.
처음에 받은 문자가 체크문자랑 다를 경우 출력해주고 체크문자로 바꿔줍니다.
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c = '1';
StringBuilder sb = new StringBuilder();
for(char ch : br.readLine().toCharArray()) {
if(c != ch) {
sb.append(ch);
c = ch;
}
}
System.out.print(sb);
}
}
728x90
반응형
'백준 > 5001 - 10000' 카테고리의 다른 글
[백준] 1213번 : HTML(JAVA) (0) | 2021.08.17 |
---|---|
[백준] 9342번 : 염색체(JAVA) (0) | 2021.08.09 |
[백준] 6721번 : Backward numbers(JAVA) (0) | 2021.08.02 |
[백준] 5489번 : Numbers(JAVA) (0) | 2021.07.24 |
[백준] 5052번 : 전화번호 목록(JAVA) (0) | 2021.07.12 |
댓글