백준 1110 - 더하기 사이클

it/알고리즘 2019. 12. 17. 22:55 Posted by 하얀나다
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
49
50
51
52
53
54
 
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));
 
        String init = br.readLine();
        String str[] = init.split("");
        int cnt = 0;
        String x = "";
 
        while (true) {
            x = cal(str);
            cnt++;
//            bw.write(cnt + " : " + x + "\n");
            
            String bi = Integer.toString(Integer.parseInt(x));
            
            str = x.split("");
            
            if (init.equals(bi)) {
                break;
            }
            
        }
        
        bw.write(cnt +"\n");
 
        bw.flush();
    }
 
    private static String cal(String[] str) {
        String x;
        if (str.length > 1) {
            int a = Integer.parseInt(str[0]) + Integer.parseInt(str[1]);
            if (a >= 10) {
                x = Integer.parseInt(str[1]) + ("" + Integer.toString(a).charAt(1));
            } else {
                x = Integer.parseInt(str[1]) + Integer.toString(a);
            }
        } else {
            x = str[0+ str[0];
        }
 
        return x;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

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

백준 1516 - 게임 개발  (0) 2019.12.17
백준 1260 - DFS와 BFS  (0) 2019.12.17
백준 1058 - 친구  (0) 2019.12.17
백준 1012 - 유기농 배추  (0) 2019.12.17
백준 10026 - 적록색약  (0) 2019.09.01