ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [java] 백준 알고리즘 1110번 더하기 사이클 풀이
    알고리즘/백준 알고리즘 2018. 5. 17. 15:27



    다른 사람들 소스를 보면 다들 쉽게 쉽게 풀었는데,


    난 항상 왜 이렇게 어렵게 푸는지 모르겠다. ㅠㅠ


    * 풀이소스


    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
    55
    56
    57
    public class Baekjoon1110 {
     
        public static void main(String[] args) throws NumberFormatException, IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            
            String n = br.readLine(); // 입력받을 정수(문제 속의 N)
            int intN = Integer.parseInt(n); // String으로 받기 때문에 int로 변환된 N
            int cycle = 0;
            int newNum = 0;
            
            /* 문제처럼 10보다 작으냐 크냐에 계산이 달라진다. */
            if(intN<10) { // 10보다 작은 경우,
                
                newNum = intN + intN; // 10보다 작을 경우, 사이클 1개는 지나야하며, 사이클 하나가 지난 값은 주어진 n값을 2번 더한 것과 같다.
                String stringNewNum = String.valueOf(newNum);
                int firstNum = intN; // 계산 속의 + 연산 사이에 오른쪽에 위치할 수
                cycle = 1// 위의 설명대로 사이클 1개 추가
                
                while(!n.equals(stringNewNum)) { // 입력한 값과 같아질 때까지 loop
                    stringNewNum = String.valueOf(newNum); 
                    int secondNum = Integer.parseInt(stringNewNum.substring(stringNewNum.length()-1)); // 사이클 중 계산되고 난 값(= 등호 오른쪽 값)의 오른쪽에 있을 숫자
                    if(firstNum == 0) {
                        stringNewNum = String.valueOf(secondNum); // 자리수가 1개일 경우에는 그대로 대입.
                    }else {
                        stringNewNum = String.valueOf(firstNum)+String.valueOf(secondNum); // + 연산의 오른쪽 수, 계산된 후의 오른쪽 수를 합친다.
                    }
                    
                    newNum = firstNum + secondNum;  // 만들어질 수
                    firstNum = secondNum; // 등식(=)으로 나온 수의 오른쪽에 있는 수는 다음 사이클에서 + 연산 값의 오른쪽 수가 된다.
                    cycle++// 1번 연산될 때마다 사이클도 자동 업
                    
                }
            }else {
                newNum = Character.getNumericValue(n.charAt(0)) + Character.getNumericValue(n.charAt(1)); // 두자릿수라서 바로 계산.
                String stringNewNum = String.valueOf(newNum); /* 원리는 위와 같다. */ 
                int firstNum = Character.getNumericValue(n.charAt(1));
                
                while(!n.equals(stringNewNum)) {
                    stringNewNum = String.valueOf(newNum); 
                    int secondNum = Integer.parseInt(stringNewNum.substring(stringNewNum.length()-1)); 
                    
                    stringNewNum = String.valueOf(firstNum)+String.valueOf(secondNum); 
                    
                    newNum = firstNum + secondNum; 
                    
                    firstNum = secondNum; 
                    cycle++;
                    
                }
            }
     
            bw.write(String.valueOf(cycle));
            bw.flush();
        }
     
    }
    cs


Designed by Tistory.