본문 바로가기
Problem Solving/Baekjoon

[백준] 17614 369 - Brute Force / Java

by graycode 2024. 3. 27.

 문제 링크

 

17614번: 369

민수는 같은 반 친구들과 369게임을 하고 있다. 369게임은 여러 명이 원형으로 둘러 앉아 시작 위치의 사람이 1을 외치며 시작된다. 이후 시계방향으로 돌아가며 2, 3, 4와 같이 1씩 증가된 수가 자

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), cnt = 0;
        for (int i = 1; i <= n; i++) cnt += contains(i);

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static int contains(int i) {
        int mod, cnt = 0;
        do if ((mod = i % 10) == 3 || mod == 6 || mod == 9) cnt++; while ((i /= 10) > 0);

        return cnt;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글