您的位置:首页 > 其它

Divide Two Integers

2015-08-03 15:16 399 查看
class Solution {
public:
int divide(int dividend, int divisor) {
bool flag=((dividend >0) ^ (divisor>0));
cout<<(dividend >0);
cout<<(divisor >0);
cout<<flag<<endl;
/*if(divisor==1) return dividend;
if(divisor==-1) return -dividend;
if(dividend==divisor) return 1;
if(dividend==-divisor) return -1;*/

uint64_t end=(unsigned int)(abs(dividend));
uint64_t sor=(unsigned int)(abs(divisor));
cout<<end<<" "<<sor<<endl;
long long res=0;
int p=0;
while(end>=sor){
res+=pow(2,p++);
end-=sor;
sor <<= 1;
//
}
//cout<<" fdfsdf"<<res<<endl;
cout<<end<<" "<<sor<<endl;

if(p){
p--;
sor >>=1;
//cout<<end<<"---"<<sor<<endl;
while(end >0 ){
//cout<<end<<" "<<sor<<endl;
while(end>=sor){
res+=pow(2,p);
cout<<end<<" "<<sor<<endl;
cout<<" fdfsdf"<<res<<endl;
end-=sor;
}
sor >>= 1;
p--;
}
}
if(!flag) cout<<"-----"<<res;
return flag?-res:res;
}
};


这题让我好厌烦,,这种判断边界。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: