수학 재미있는 문제 - 귀속법 멱 구하기

   1: #include "stdio.h"
   2:  
   3: unsigned long myPow(int m, int n)
   4: {
   5:     unsigned long tmp;
   6:  
   7:     if(n == 0) return 1;
   8:  
   9:     if(n == 1) return m;
  10:  
  11:     if(n % 2 == 0) {
  12:         tmp = myPow(m, n / 2);
  13:         return tmp * tmp;
  14:     }
  15:  
  16:     if(n % 2 != 0)
  17:         return m * myPow(m, n - 1);
  18: }
  19:  
  20: int main()
  21: {
  22:     int m, n;
  23:     printf("Please input the bottom number
"
);
  24:     scanf("%d", &m);                            /* m*/
  25:     printf("Please input the exponent number
"
);
  26:     scanf("%d", &n);                            /* n*/
  27:     printf("The result of power(m,n) is %ld
"
, myPow(m, n)); /* mn*/
  28:     return 0;
  29: }

좋은 웹페이지 즐겨찾기