-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivideTwoIntegers.cpp
More file actions
executable file
·40 lines (33 loc) · 1.01 KB
/
DivideTwoIntegers.cpp
File metadata and controls
executable file
·40 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// DivideTwoIntegers.cpp
// leetcode
//
// Created by witwolf on 11/19/14.
// Copyright (c) 2014 witwolf. All rights reserved.
//
#include <stdio.h>
#include <iostream>
using namespace std;
int divide(int dividend, int divisor) {
dividend = dividend < 0 ? -dividend : dividend;
divisor = divisor < 0 ? -divisor : divisor;
int quotient = 0;
while(dividend >= divisor){
int c = divisor;
for(int i = 0 ; dividend>=c ; ++i,c <<= 1 ){
dividend -= c;
quotient += 1 << i ;
}
}
return dividend ^ divisor >> 31 ? -quotient : quotient ;
}
int main(int argc,char **argv){
std::cout << divide(18, 5) << std::endl;
std::cout << divide(5,18) << std::endl;
std::cout << divide(-18, -5) << std::endl;
std::cout << divide(-5,18) << std::endl;
std::cout << divide(-18, 5) << std::endl;
std::cout << divide(5,-18) << std::endl;
std::cout << divide(18, -5) << std::endl;
std::cout << divide(-5, -18) << std::endl;
}