码迷,mamicode.com
首页 > 其他好文 > 详细

Divide Two Integers

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

标签:array   leetcode   

题目

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

标签:array   leetcode   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!