본문 바로가기

IT/알고리즘

백준 6단계(1065)

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
public class Main {
    
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int n = Integer.parseInt(br.readLine());
        int cnt = 0;
        for(int i = 1; i <= n; i++) {
            if(check(i)) cnt++;
        }
        bw.write(String.valueOf(cnt));
        bw.flush();
        bw.close();
        br.close();
    }
    
    public static boolean check(int i) {
        if(i < 100return true;
        
        int a = i % 10//일자리
        int b = i/10 % 10//십자리
        int c = i/100 % 10//백자리
        
        //여기서 절대값으로 처리하면 975같은 숫자들 처리가 불가능...
        int first = b - c;
        int second = a - b;
        
        if(i >= 1000return false;
 
        if(first == second) {
//            System.out.println(first + " " + second + " " + i);
//            System.out.println("\n");
            
            return true;
        } 
        
        return false;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

숫자가 등차수열이면 cnt++으로 입력된 숫자까지 등차수열이 몇개가 존재하는지 찾는 알고리즘.

십자리와 일자리를 뺀 first, 백자리와 십자리를 뺀 second가 같으면 true를 리턴해줘서 true면 cnt++.

 

 

반응형

'IT > 알고리즘' 카테고리의 다른 글

백준 7단계(11720)  (0) 2019.12.07
백준 7단계(11654)  (0) 2019.12.07
백준 6단계(4673)  (0) 2019.12.07
백준 6단계(15596)  (0) 2019.12.07
백준 5단계(4344)  (0) 2019.11.28