Lined Notebook

[1217] [Java] 거듭 제곱

by HeshAlgo

<거듭 제곱>

문제 설명

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14dUIaAAUCFAYD&categoryId=AV14dUIaAAUCFAYD&categoryType=CODE

 

내 생각

involution() 메서드에서 재귀를 사용할 때 n의 값을 곱할 때 고정값으로 곱했어야 했는데 n*=n을 사용해서 값이 엄청 크게 나와서 당황했습니다..ㅎㅎ.. 그것만 빼면 문제를 풀 때 금방 풀 수 있었습니다. 

 

푼 시간

12분 01초

 

작성 코드

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
// 거듭 제곱
import java.io.FileInputStream;
 
public class E1217 {
 
    public static void main(String args[]) throws Exception {
        String path = "input.txt";
        System.setIn(new FileInputStream(path));
        Scanner sc = new Scanner(System.in);
        
        
        for(int test_case = 1; test_case <= 10; test_case++) {
            int T = sc.nextInt();
            int N = sc.nextInt(); 
            int M = sc.nextInt();
            int fixNum = N;
            int answer = involution(fixNum, N, M);
            System.out.println("#" + T + " " + answer);
            
        }
        
    }
 
    public static int involution(int fixNum, int n, int m) {
        if (m == 1)
            return n;
        n *= fixNum;
        m -= 1;
        
        return involution(fixNum, n, m);
    }
    
}
 
 

 

실행 결과

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기