‡ CODING TEST STUDY ‡/º 백준

[백준 | Java Bronze I] (#1110) 더하기 사이클

Trudy | 송연 2024. 6. 25. 01:58

문제

https://www.acmicpc.net/problem/1110


이슈

tmp 배열을 n이랑 같게  복사해서 넣을 생각이었는 데, tmp = n을 하다보니 tmp가 n을 참조하게 돼서 tmp를 수정하면 n까지 수정하는 문제가 발생했다. 

밑에처럼 고치니까 해결 됨! 

//문제의 코드
int[] tmp = n;

//고친 코드
int[] tmp = {n[0], n[1]};

 

최종 코드

package week8.baek;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B1110 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        int[] n = new int[2];
        if(Integer.parseInt(input) < 10) {
            n[0] = 0;
            n[1] = Integer.parseInt(input);
        }
        else {
            n[0] = Integer.parseInt(input) / 10;
            n[1] = Integer.parseInt(input)  % 10;
        }


        int count = 1;

        int[] tmp = {n[0], n[1]};
        int sum = tmp[0] + tmp[1];
        tmp[0] = tmp[1];
        tmp[1] = sum % 10;
//        System.out.println(tmp[0] + " " + tmp[1]);

        while(!(tmp[0] == n[0] && tmp[1] == n[1])){
            sum = tmp[0] + tmp[1];
            tmp[0] = tmp[1];
            tmp[1] = sum % 10;
//            System.out.println(tmp[0] + " " + tmp[1]);
            count++;
        }
        System.out.println(count);
    }
}