μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œν’€μ΄

[λ°±μ€€]Q1110 λ”ν•˜κΈ° 사이클(μžλ°”)

JihyunLee 2019. 9. 29. 12:55
λ°˜μ‘ν˜•

λ°±μ€€ 1110 번 λ”ν•˜κΈ° 사이클 문제λ₯Ό μžλ°”λ₯Ό μ΄μš©ν•΄μ„œ ν’€μ—ˆλ‹€.

λ”±νžˆ μžλ£Œκ΅¬μ‘°κ°™μ€κ²ƒμ΄ ν•„μš”ν•˜μ§€ μ•Šμ•˜λ‹€.

 

 

 

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
import java.util.*;
 
public class Q1110{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
 
        int N = scan.nextInt();
        int cnt=0;
        int next =N;
        while(true){
            next = func(next);
            cnt++;
            if(N==next){
                break;
            }
        }
        
        System.out.println(cnt);
 
    }
 
    private static int func(int n){
        int next_num=0;
        if(n>=10){
            //sum of each position
            int position_sum = n/10 + n%10;
            int position_sum_last = position_sum%10;
        next_num = (n%10)*10 + position_sum_last;
        }
 
        if(n<10){
            next_num = n*10 + n; 
        }
 
        return next_num;
    }
}
cs

 

μ•Œκ²Œ 된 것

char -> int 둜 λ³€ν˜•ν•  λ•Œ -0 같은 연산을 ν•΄μ£Όλ©΄ λœλ‹€κ³  ν•˜λŠ”λ° λ‚˜λŠ” 잘 μ•ˆλ˜μ—ˆλ‹€.

그리고 두 자리수의 각 자리 합을 κ΅¬ν• λ•Œ N%10 + N/10을 ν•˜λ©΄ μ‰½κ²Œ ꡬ할 수 μžˆμ—ˆλ‹€.

λ°˜μ‘ν˜•