数学趣题——递归法求幂

   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\n");
  24:     scanf("%d", &m);                            /*输入底数m*/
  25:     printf("Please input the exponent number\n");
  26:     scanf("%d", &n);                            /*输入指数n*/
  27:     printf("The result of power(m,n) is %ld\n", myPow(m, n));        /*输出计算结果mn*/
  28:     return 0;
  29: }

你可能感兴趣的:(递归)