본문 바로가기
알고리즘(C언어)/이것이 자료구조+알고리즘이다(박상현)_알고리즘

(6)[Chapter12]분할정복_2_거듭제곱

by 코잼민 2024. 9. 6.

##이론 노트 :

##코드 :

 

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
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable: 4996)
 
typedef unsigned long ULONG;
 
//Base : 밑 , Exponent : 지수
ULONG Power(int Base, int Exponent) 
{
    //1_ 종료조건 (지수가 0 , 1 일 때,)
    if (Exponent == 0) {
        return 1;
    }
    else if (Exponent == 1) {
        return Base;
    }
 
    //2_ 지수가 짝수일 떄,
    if (Exponent % 2 == 0
    {
        ULONG NewBase = Power(Base, (Exponent) / 2);
 
        return NewBase * NewBase;
    }
    else {
        ULONG NewBase = Power(Base, (Exponent - 1/ 2);
        
        return NewBase * NewBase * Base;
    }
 
 
}
 
int main() 
{
 
    int b, e;
 
    printf("밑을 입력하시오 : ");
    scanf("%d"&b);
    printf("지수를 입력하시오 : ");
    scanf("%d"&e);
 
    printf("%d^%d = %lu\n", b, e, Power(b, e));
 
 
    return 0;
}
cs