21.05.18 기록

최대 1 분 소요

  • 백준 알고리즘 1065 풀이 완료

    • 내가 푼 1065 풀이 (메모리 14.6MB, 시간 148ms로 통과)
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class B1065 {
        public static void main(String[] args) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                int N = Integer.parseInt(br.readLine());
                System.out.println(countX(N));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static int countX(int N) {
            int res = 0;
            int count = 0;
    
            for(int i = 1; i <= N; i++) {
                if(i >= 100 && i < 1000) {
                    res = ((i / 100) - (i % 100 / 10)) - ((i % 100 / 10) - (i % 10));
                }
    
                if(i == 1000) res = -1;
    
                if(res == 0) count++;
            }
    
            return count;
        }
    }
    


  • 풀이를 참고하여 다듬은 countX 함수 (메모리 14.6MB, 시간 152ms로 통과)

    public static int countX(int N) {
        int res = 0;
        int count = 99;

        if(N < 100) return N;
        else {
            if(N == 1000) N = 999;

            for(int i = 100; i <= N; i++) {
                res = ((i / 100) - (i % 100 / 10)) - ((i % 100 / 10) - (i % 10));
                if(res == 0) count++;
            }
        }

        return count;
    }


카테고리:

업데이트: