문제.

알고리즘.
그냥 문자열 배열에 넣고 3,6,9 포함할때마다 count 값을 증가시킨 후 count 값만큼 문자열 배열 내의 숫자를 -로 바꿔준 후 count 초기화
코드.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Example {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[] game = new String[N];
for(int i=1;i<=N;i++ ) {
game[i-1] = Integer.toString(i);
}
for(int i=0;i<N;i++) {
int count = 0;
for(int j=0;j<game[i].length();j++) {
if(game[i].charAt(j) == '3') {
count++;
}
if(game[i].charAt(j) == '6') {
count++;
}
if(game[i].charAt(j) == '9') {
count++;
}
}
if(count!=0) {
game[i] = "";
for(int k=0;k<count;k++) {
game[i] += "-";
}
}
}
for(int i=0;i<N;i++) {
System.out.printf("%s ", game[i]);
}
br.close();
}
}
결과.

소감.
너무 코드가 불필요하게 길다. stream을 이용하면 더 빠를텐데...
'코딩테스트' 카테고리의 다른 글
| SWEA 1213 : String (0) | 2024.03.05 |
|---|---|
| SWEA 2005 : 파스칼의 삼각형 (0) | 2024.02.23 |
| SWEA 1859 : 백만장자 프로젝트 (0) | 2024.02.22 |
| 백준 2751 : 수 정렬하기 2 (0) | 2024.02.21 |
| 백준 1018번 : 체스판 다시 칠하기 (0) | 2024.02.21 |