LeetCode OJ-- Divide Two Integers *
https://oj.leetcode.com/problems/divide-two-integers/
在不使用乘法、除法、求余的情况下计算除法。
使用减法计算,看看减几次。
刚开始寻思朴素的暴力做,然后超时了。
于是开始增大每次的被减数
但是溢出了。
2的32次方=4294967296(无符号),带符号再除以2,负数比正数多一个,-2147483648~+2147483647
所以int的范围就是 -2147483648~2147483648.
于是当输入中有 -2147483648的时候,对于 abs()函数返回结果就溢出了,求得后的值还是 -2147483648.
于是用 long long 来作为中间数据类型。并且不用abs()函数了。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; class Solution {
public:
int divide(int dividend, int divisor) { long long dividend_l = dividend;
long long divisor_l = divisor; bool positive = true;
if(dividend_l>&&divisor_l< || dividend_l< && divisor_l>)
positive = false; if(dividend_l<)
dividend_l = -dividend_l;
if(divisor_l<)
divisor_l = -divisor_l; if(dividend == || dividend_l< divisor_l)
return ;
if(dividend == divisor)
return ; int ans = ; while(dividend_l>=divisor_l)
{
ans += subDivide(dividend_l,divisor_l);
} if(positive == false)
ans = -ans; return ans;
}
int subDivide(long long ÷nd,long long divisor)
{
int timesSum = ;
long times = ;
long long _divisor = divisor;
while(_divisor> && dividend>=_divisor)
{
dividend = dividend - _divisor;
_divisor += _divisor;
timesSum += times;
times += times; //means _divisor is how much copies of divisor
}
return timesSum;
}
}; int main()
{
class Solution mys;
cout<<mys.divide(-,-);
}
LeetCode OJ-- Divide Two Integers *的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- 嵌入式Linux环境搭建备忘
嵌入式Linux开发平台搭建步骤: 1.安装宿主机Linux系统 如果选用最新的Linux发行版,应改主意其他软件是否能很好的兼容. 2.安装交叉编译器 交叉编译器的版本很多,一般到芯片厂家官网下载官 ...
- hihocoder#1098 : 最小生成树二·Kruscal算法
#1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...
- linux 查看CPU内存 网络 流量 磁盘 IO
使用vmstat命令来察看系统资源情况 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? Q: 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? A: 在命 ...
- [BZOJ1187]神奇游乐园(插头DP)
Description 题意给定一个矩阵,每个格子有权值,在[-1000.1000]内,求一条回路使得回路经过权值和最大,每个格子最多经过一次 2≤n≤100,2≤m≤6 Code #include ...
- 笔记-python-standard library-19.2 json
笔记-python-standard library-19.2 json 1. JSON简介 JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级 ...
- 《鸟哥的Linux私房菜》学习笔记(5)——权限管理
一.权限的基本概念 权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...
- python-PIL模块的使用
PIL基本功能介绍 from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img. ...
- cogs:1619. [HEOI2012]采花/luogu P2056
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- loj2061 「HAOI2016」放棋子
答案就是错排数 n = int(input()) f = [0] * 205 f[0] = 1 for i in range(2, n+1): f[i] = (i-1) * (f[i-1] + f[i ...
- SXCPC2018 nucoj2004 国王的怪癖
可持久化trie.考场上我脑补了一个trie树合并也A了 #include <iostream> #include <cstring> #include <cstdio& ...