首页 > 其他 > 详细

Divide Two Integers

时间:2014-06-16 19:07:37      阅读:347      评论:0      收藏:0      [点我收藏+]

题目

Divide two integers without using multiplication, division and mod operator.

方法

将除数倍加,直到大于被除数。
	    public int divide(int dividend, int divisor) {
	        int flag = 0;
	        if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)){
	            flag = 1;
	        }
	        
	        long divid = Math.abs((long)dividend);
	        long divis = Math.abs((long)divisor);
	        
	        long n;
	        
	        int quotient = 0;
	        
	        int quo = 0;
	        while(divid >= divis){
	            n = divis;
	            quo = 1;
	            while(n + n <= divid){
	                n = n + n;
	                quo = quo + quo;
	            }
	            divid =divid - n;
	            quotient += quo;
	        }
	        
	        if(flag == 1){
	            return -quotient;
	        }
	        return quotient;
	    }



Divide Two Integers,布布扣,bubuko.com

Divide Two Integers

原文:http://blog.csdn.net/u010378705/article/details/31347195

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!