[SWEA] 1926

ninano05·2026년 4월 3일

완전 탐색

  • 숫자 하나하나 3,6,9가 포함되어 있는지 검사한다.
  • 검사한 후 그 수 만큼 -을 출력한다.
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(br.readLine());

        for(int i=1; i<=N; i++) {
            int num = clap(i);
            if(num > 0) {
                for(int j=0; j<num; j++) {
                    sb.append("-");
                }
                sb.append(" ");
            } else {
                sb.append(i).append(" ");
            }
        }
        System.out.println(sb);
        br.close();
    }

    // 3,6,9가 포함된 수 반환 메서드
    public static int clap(int n) {
        String s = String.valueOf(n);
        int clap = 0;

        for(int i = 0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(c == '3' || c == '6' || c == '9') clap++;
        }
        return clap;
    }

}
profile
초보 개발자

0개의 댓글