[알고리즘]_[BeakJoon]- 2331.반복수열

SAPCO·2022년 11월 24일
0

📍 1. 문제

📍 2. 풀이

리스트에 넣고, 리스트에 해당 숫자가 존재하면 해당숫자의 index 출력.

📍 3. 코드

  1. 인접리스트 + DFS-Recursive
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());
	
		int a = Integer.parseInt(st.nextToken());
		int n = Integer.parseInt(st.nextToken());
				
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(a);

		while(true) {
			String num = Integer.toString(a);
			a = 0;
			for(int i = 0; i < num.length(); i++) {
				a += Math.pow((int)num.charAt(i) - '0', n);
			}

			if(list.indexOf(a) != -1) {
				System.out.println(list.indexOf(a));
				break;
			}else {
				list.add(a);				
			}
		}
	}
}
profile
SAP CO

0개의 댓글