zoukankan      html  css  js  c++  java
  • 冪運匴

    簊亍书丄旳峛孒,①個潲嶶攺進了①些旳冪運匴,加了一个简单的check,以及改成了位运算。

       1:  #include <stdio.h>
       2:  #include <stdlib.h>
       3:  #include <limits.h>
       4:   
       5:  unsigned long power(int base,int n)
       6:  {
       7:      //a simple check
       8:      if(base>=2 && n>=32)
       9:      {
      10:          printf("the result will exceed the limit of long:%u\n",ULONG_MAX);
      11:          exit(-1);
      12:      }
      13:   
      14:      if(n==0)
      15:      {
      16:          return 1;
      17:      }
      18:      else if(n==1)
      19:      {
      20:          return base;
      21:      }
      22:      else if((n & 1)==0)
      23:      {
      24:          return power(base*base,n>>1);
      25:      }
      26:      else if((n & 1)!=0)
      27:      {
      28:          return power(base*base,n>>1)*base;
      29:      }
      30:      else
      31:      {
      32:          //should not get here
      33:          return -1;
      34:      }
      35:  }
  • 相关阅读:
    Linux和windows 查看程序、进程的依赖库的方法
    ida pro 使用
    sql server相关
    nginx 日志log_format格式
    .yml文件格式
    svn加锁设置
    包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解
    visual studio 2017 (vs2017安装)
    OpenResty编译安装
    微信扫描二维码登录网站技术原理
  • 原文地址:https://www.cnblogs.com/dancewithautomation/p/2559448.html
Copyright © 2011-2022 走看看