快速乘

 

 1 作者:handsomehow
 2 链接:https://www.nowcoder.com/discuss/69932?type=2&order=0&pos=39&page=1
 3 来源:牛客网
 4 
 5 int mul(int a, int b){
 6       int sig = 1;
 7       if(b < 0){
 8         sig *= -1;
 9           b = -b;
10     }
11       if(a < 0){
12         sig *= -1;
13           a = -a;
14     }
15     int ret = 0;
16       while(b){
17         if(b&1){
18             ret = ret + a;
19         }
20           a += a;
21           b >>= 1;
22     }
23       return ret * sig;
24 }

 

posted @ 2018-03-23 11:03  Kiven#5197  阅读(159)  评论(0编辑  收藏  举报