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:  }
  • 相关阅读:
    (十一)设置关闭多核cpu的核
    (十)修改开发板主频
    (九)How to use the audio gadget driver
    (8)全志A64查看寄存器
    内存溢出问题配置
    百度数据库优化经验
    如何让sql运行的更快
    百度性能优化
    spring ioc原理
    JNDI
  • 原文地址:https://www.cnblogs.com/dancewithautomation/p/2559448.html
Copyright © 2011-2022 走看看