注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

火车的家

Put first thing first

 
 
 

日志

 
 

2014.12.13 [转] Implement the divide of two integers without using the divide operator.  

2014-12-13 13:39:37|  分类: 技术博客 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Question:
Implement the divide of two integers without using the divide operator.

Answer:
Double the divisor everytime until it is greater than numerator.Keep track of the doubling. Apply this recursively until the subtraction is less than divisor.
E.g.
40/3-> 40/6->40/12->40/24 : Keep track of doubling: 2 power 3 = 8
16/3 - > 16/6->16/12: Keep track of doubling 2 Power 2 = 4
4/3-> 1 Stop here because 1 less than 3: Keep track of doubling i.e. 2 power of zero: 1

8 + 4 +1 =13 factor remainder = 1

This can be further improved. In the second step, you can start from 12(largest doubled value which is less than 16) onwards and not from 3 again.

40/3-> 40/6->40/12->40/24 : Keep track of doubling: 2 power 3 = 8
16/12: Keep track of doubling 2 Power 2 = 4
4/3-> 1 Stop here because 1 less than 3: Keep track of doubling i.e. 2 power of zero: 1
Total steps: 5

  评论这张
 
阅读(263)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017