Á¤¼ºÈÆ
    ½Ç½À ÇÁ·Î±×·¥ ¿¹)
power_compare.txt [1 KB]   power_callreturn.txt [1 KB]  
  http://



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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
double slow_power(double x, int n)
{
    int i;
 
    double r = 1.0;
 
    for (i = 0; i < n; i++)
        r = r * x;
    return (r);
}
 
double power(double x, int n)
{
    if (n == 0return 1;
    else if ((n % 2== 0)
        return power(x*x, n / 2);
    else return x * power(x*x, (n - 1/ 2);
}
 
int main(void)
{
    double  x, value_slow, value;
    int n;
 
    x = 2;
    n = 10;
 
    value_slow = slow_power(x, n);
    value = power(x, n);
 
 
    printf("slow_power(%lf,%d) = %lf \n", x, n, value_slow);
    printf("power(%lf, %d) = %lf \n", x, n, value);
}
cs

 

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
#include <stdio.h>
#include <stdlib.h>
 
double power(double x, int n)
{
    double value, result;
 
    printf("call power(%lf, %d) \n", x, n);
    if (n == 0) {
        printf("return 1\n");
        return 1;
    }
    else if ((n % 2== 0) {
        value = power(x*x, n / 2);
        printf("return %lf \n", value);
        return value;
    }
    else {
        value = power(x*x, (n - 1/ 2);
        result = x * value;
        printf("return %lf * %lf \n", x, value);
        return result;
    }
}
 
int main(void)
{
    double  x, value_slow, value;
    int n;
 
    x = 2;
    n = 10;
 
    value = power(x, n);
 
    printf("power(%lf, %d) = %lf \n", x, n, value);
}
cs

  µî·ÏÀÏ : 2020-09-13 [17:26] Á¶È¸ : 2246 ´Ù¿î : 771   
 
¡â ÀÌÀü±Û(½Ç½À 2) ¼øÈ¯ // 4ÁÖÂ÷ ¿ÀÇÁ¶óÀÎ ½Ç½À
¡ä ´ÙÀ½±Û½Ç½À ÇÁ·Î±×·¥ ¿¹)
ÀڷᱸÁ¶ ½Ç½À°Ô½ÃÆÇ
¹øÈ£ ¨Ï Á¦ ¸ñ
l[Âü°í] ±³Àç¿¡ ÀÖ´Â ¼Ò½ºÄÚµå
l½Ç½À ¸ñÂ÷
27 ¦¦❶ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
26    ¦¦❷ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
25       ¦¦❸ 💯 ±¸Á¶Ã¼ µ¥ÀÌÅÍÀÇ swap (Ãß°¡)¨Õ
24          ¦¦❹ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // 1) ±¸Á¶Ã¼ º¹»ç©
23             ¦¦❺ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // 2) ±¸Á¶Ã¼ ¹è¿­ÀÇ Ã¹¹øÂ°¿Í µÎ¹øÂ°¸¦ swap
22                ¦¦❻ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // 3) ±¸Á¶Ã¼ Àüü copy
21                   ¦¦❼ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // ±¸Á¶Ã¼ ¹öºí Á¤·Ä
20 l(½Ç½À 2) ¼øÈ¯ // 4ÁÖÂ÷ ¿ÀÇÁ¶óÀÎ ½Ç½À¨Õ
19 ¦¦❶ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
18    ¦¦❷ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
17       ¦¦❸ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
16          ¦¦❹ 💯 power ´Ù¸¥ ¼øÈ¯ ±¸Çö (Ãß°¡)¨Õ
15             ¦¦❺ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // power ±¸Çö
14                ¦¦❻ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // ±¸±¸´Ü ±¸Çö
13 l(½Ç½À 1) ¼±ÅÃÁ¤·Ä // 3ÁÖÂ÷ ¿ÀÇÁ¶óÀÎ ½Ç½À
12 ¦¦❶ ¹öºí Á¤·Ä
11    ¦¦❷ ¹öºí Á¤·Ä (¸ÆºÏ¿¡¼­ ½ÇÇàÇÒ ¶§)
10 ¦¦❶ l½Ç½À ÇÁ·Î±×·¥ ¿¹)
9    ¦¦❷ 💯 ¹®ÀÚ¿­ Á¤·Ä (Ãß°¡)
8       ¦¦❸ (Ãß°¡) ½Ç½À ÇÁ·Î±×·¥ ¿¹) // ¼±ÅÃÁ¤·Ä

[1][2][3][4][5]