验证中...
Languages: C/C++
Categories: 编程语言基础
Latest update 2019-10-12 14:29
divide2Integers.cpp
Raw Copy
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int divide(int dividend, int divisor) {
long long m = abs((long long)dividend);
long long n = abs((long long)divisor);
if(m < n)
{
return 0;
}
long long res = 0;
while (m >= n)
{
long long n1 = n;
long long p = 1;
while (m >= (n1 << 1))
{
n1 <<= 1;
p <<= 1;
}
m -= n1;
res += p;
}
if((dividend < 0) ^ (divisor < 0))
{
res = 0 - res;
}
if(res > INT_MAX)
{
res = INT_MAX;
}
if(res < INT_MIN)
{
res = INT_MIN;
}
return res;
}
int main()
{
cout << divide(-2147483648, -1) << endl;
return 0;
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search