Notice
Recent Posts
Recent Comments
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Tags more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฒ์ž๋ผ๋‚˜๋Š”์ฒญ๋…„

[๋ฐฑ์ค€]Q1110 ๋”ํ•˜๊ธฐ ์‚ฌ์ดํด(์ž๋ฐ”) ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด

[๋ฐฑ์ค€]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์„ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•