由买买提看人间百态

topics

全部话题 - 话题: divisor
1 2 3 下页 末页 (共3页)
p*****2
发帖数: 21240
1
来自主题: JobHunting版 - 怎样求common divisor 最快
下边这道题,我优化了半天也不行。有标准求common divisor的快速算法吗?
By 2312 there were n Large Hadron Colliders in the inhabited part of the
universe. Each of them corresponded to a single natural number from 1 to n.
However, scientists did not know what activating several colliders
simultaneously could cause, so the colliders were deactivated.
In 2312 there was a startling discovery: a collider's activity is safe if
and only if all numbers of activated colliders are pairwise relatively prime
to each other (two numbers a... 阅读全帖
p*****2
发帖数: 21240
2
来自主题: JobHunting版 - 怎样求common divisor 最快

简单的话就是求两个数的common divisor.
x******a
发帖数: 6336
3
来自主题: JobHunting版 - 怎样求common divisor 最快
先求gcd,然后求gcd的所有divisor?
p*****2
发帖数: 21240
4
来自主题: JobHunting版 - 怎样求common divisor 最快

这题只要求common divisor 或 1 或 非1就可以了。有什么快的吗?
z*d
发帖数: 18
5
来自主题: JobHunting版 - 问一道 ama的除法题
How about this one? Any suggestions or corrections will be highly
appreciated.
#include
#include
int divide (int x, int y) {
long long dividend, divisor, sign_dividend, sign_divisor;
long long low, middle, high;
int quotient;
/* check whether divisor is 0 */
if (y == 0) {
printf("Dvided by 0 error! \n");
assert(0);
}

/* unsign both dividend and divisor and mark the signess */
if (x < 0) {
dividend = -x;
sign... 阅读全帖
C***U
发帖数: 2406
6
来自主题: JobHunting版 - leetcode上的2个整数相除
我把dividend和divisor存到x y里面都是unsigned int了
下面这个代码对了 通过leetcode的验证了 只不过写的很丑
把最小的数拿出来单独处理了
int divide(int dividend, int divisor) {
int flag = 1;
unsigned long int x;
unsigned long int y;
unsigned long int z;
unsigned int i = 0;
unsigned int count = 0;
int result = 0;
if(!divisor) {
cout << "could not be divided by 0" << endl;
return 0;
}
if(dividend == -2147483648) {
if(divisor > 0) {
dividend += divisor;
result ... 阅读全帖
l***4
发帖数: 1788
7
来自主题: JobHunting版 - 两整数相除问题
leetcode上的divide integer题,小弟的code总是在2147483647, 1这个case上用时过
长,请高手帮忙挑挑bug 拜谢~
code如下:
int divide(int dend, int dor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
long dividend=dend;
long divisor=dor;
if(dividend==0||divisor==0)
return 0;
int negate=1;
if(dividend<0){
dividend=-dividend;
negate=-negate;
}
if(divisor<0){
divisor=-divisor;
... 阅读全帖
t******n
发帖数: 2939
8
☆─────────────────────────────────────☆
l63 (l63) 于 (Thu May 23 00:34:22 2013, 美东) 提到:
假设素数只有有限个, 记为 p_1,p_2,...,p_k
考察 N = p_1*p_2*...*p_k + 1
可知: 对于任意i = 1,2,3,...,k, p_i 不能整除 N
由素数的定义:
a是素数 <=> a是大于1的自然数, 且a不被任何小于a的素数整除
可知: N是素数
这与素数只有p_1,p_2,...,p_k矛盾.
故假设不成立.
所以素数有无穷多个.
☆─────────────────────────────────────☆
l63 (l63) 于 (Thu May 23 00:37:03 2013, 美东) 提到:
在承认素数的这个等价定义 (即 a是素数 <=> a是大于1的自然数, 且a不被任何小于a
的素数整除) 的前提下, 居然有人会认为这个证明是错的, 或者是不完备的.
我实在不能理解.
求问一下大家, 是不是有的人的脑子天生有缺陷, 根本怎么教都不会明白... 阅读全帖
f**********t
发帖数: 1001
9
来自主题: JobHunting版 - leecode上的divide two integers问题
我的方法如下:
int divide(int dividend, int divisor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (divisor == 0)
return 0;
int sign = 1;
if (dividend * divisor < 0)
sign = -1;
if (dividend < 0)
dividend *= -1;
if (divisor < 0)
divisor *= -1;
int result = 0;
int tmp;
int tmpret;
while (dividend >= divisor) {
tmp = diviso... 阅读全帖
o**f
发帖数: 76
10
来自主题: JobHunting版 - leetcode: Divide Two Integers 怎么做?
Line 2/3 ignore sign of dividend/divisor.
Line 5 to 12 try to remove divisor (or multiple of
divisors) from dividend, until a becomes < b.
Note that line 8 c<<=1 is bit operation and so does
line 10 ret += 1 < Line 14 check signs of dividend/divisor, if they are
same, return ret. Otherwise, return -1 * ret.
1 int divide(int dividend, int divisor) {
2 long long a = abs((double)dividend);;
3 long long b = abs((double)divisor);
4
5 long long ret = 0;
6 while (a >= b) {
7 ... 阅读全帖
s******i
发帖数: 236
11
来自主题: JobHunting版 - 求教EA一道面试题
https://www.hackerrank.com/challenges/unfriendly-numbers
There is one friendly number and N unfriendly numbers. We want to find how
many numbers are there which exactly divide the friendly number, but does
not divide any of the unfriendly numbers.
Input Format:
The first line of input contains two numbers N and K seperated by spaces. N
is the number of unfriendly numbers, K is the friendly number.
The second line of input contains N space separated unfriendly numbers.
Output Format:
Output the a... 阅读全帖
a***e
发帖数: 413
12
来自主题: JobHunting版 - Divide Two Integers OJ和CCP150的做法
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
这个Ccp150上那种做法(也就是我一开始做的直接减的方法会TLE)
是不是说明现在面试要求比以前高很多?
还在试图在原来的code上面改变以加快
class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor==0) return INT_MAX;
int r=0;
if (dividend==0) return r;
int flag=sign(dividend)*sign(divisor);
dividend=abs(dividend);
divisor=abs(divisor);
dividend=dividend-divisor;
w... 阅读全帖
a***e
发帖数: 413
13
来自主题: JobHunting版 - Divide Two Integers OJ和CCP150的做法
通过的code,但不清楚怎么能证明这个是对的,有人知道怎么证明吗?
长除法也可以写,但没这么简洁。
class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor==0) return INT_MAX;
long long r=0;
if (dividend==0) return r;
if (-1==divisor) return (-(long long int)dividend>INT_MAX? INT_MAX :
-(long long int)dividend);
long long a = dividend>=0?dividend:-(long long)dividend;
long long b = divisor>=0?divisor:-(long long)divisor;

while(a>=b)
{
long ... 阅读全帖
d**e
发帖数: 6098
14
来自主题: JobHunting版 - Amazon kindle team电面
something like this ?
if (divisor == 0)
throw exception
if (diviend == 0)
return 0
boolean negative = (divisend > 0 && divisor < 0)
|| (divisend < 0 && divisor > 0);
int result = 0;
int sum = 0;
while((sum + divisor) >= divisor) {
sum += divisor;
result++;
}
return (negative ? result * (-1) : result);
C***U
发帖数: 2406
15
来自主题: JobHunting版 - leetcode上的2个整数相除
不是科班出生 写代码 比较丑 别取笑我
int divide(int dividend, int divisor) {
int flag = 1;
unsigned int x;
unsigned int y;
unsigned int z;
unsigned int i = 0;
unsigned int count = 0;
int result = 0;
if(!divisor) {
cout << "could not be divided by 0" << endl;
return 0;
}
if(dividend < 0) {
flag = -1;
dividend = - dividend;
}
if(divisor < 0) {
if(flag < 0)
flag = 1;
else
flag = -1;
divi... 阅读全帖
c*******r
发帖数: 309
16
来自主题: JobHunting版 - 关于Divide a integer
我看了下这题的code,看了半天没搞懂这里边的位移运算是在干嘛。 为啥不能直接
while loop里每次a-b然后count次数?
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function

int a = Math.abs(dividend);
int b = Math.abs(divisor);

//b maybe Integer.MIN_VALUE
boolean neg = (dividend > 0 && divisor < 0) || (dividend < 0 &&
divisor > 0);
if (divisor == 0) {

return Integer.MAX_VALUE;
... 阅读全帖
g***j
发帖数: 1275
17
来自主题: JobHunting版 - divide two integers
class Solution {
如下的code可以过,但是有个疑问,为啥,我吧下面的long long的部分改成int,就过
不了了啊?
public:
int divide(int dividend, int divisor) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(divisor == 1) return dividend;
if(divisor == -1) return -1*dividend;
int sign = 1;

if(dividend < 0) sign = -1*sign;
if(divisor < 0) sign = -1*sign;

long long remain = abs((long long)dividend); // ???

... 阅读全帖
w*******8
发帖数: 10
18
来自主题: JobHunting版 - M 家电面
public class Solution {
public int divide(int dividend, int divisor) {
int a = Math.abs(dividend);
int b = Math.abs(divisor);
boolean neg = (dividend > 0 && divisor < 0) || (dividend < 0 &&
divisor > 0);
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (divisor == Integer.MIN_VALUE) {
return (dividend == Integer.MIN_VALUE) ? 1 : 0;
}
if (dividend == Integer.MIN_VALUE) {
if (neg) {
... 阅读全帖
a***e
发帖数: 413
19
来自主题: JobHunting版 - Divide Two Integers OJ和CCP150的做法
看到soulmachine的答案说
‘最简单的方法,是不断减去被除数。在这个基础上,可以做一点优化,每次把被除数
翻倍,从
而加速。’
不太懂为啥可以用这个翻倍除数来加速。看那个code也是有点晕。
int divide(int dividend, int divisor) {
// 当dividend = INT_MIN 时,-dividend 会溢出,所以用long long
long long a = dividend >= 0 ? dividend : -(long long)dividend;
long long b = divisor >= 0 ? divisor : -(long long)divisor;
// 当dividend = INT_MIN 时,divisor = -1 时,结果会溢出,所以用long long
long long result = 0;
while (a >= b) {
long long c = b;
for (int i = 0; a >= c; ++i, c <<= 1) {
a -= c;
result += 1 << i;
}
}
... 阅读全帖
t**i
发帖数: 314
20
来自主题: JobHunting版 - 问一个facebook的电面题
在你的基础上做了一下小修改的java code,通过了leetcode的测试:
public int divide(int dividend, int divisor) {
assert(divisor != 0);
boolean bNeg = false;
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0))
bNeg = true;
long la = dividend;
long ula = la < 0 ? -la : la;
long lb = divisor;
long ulb = lb < 0 ? -lb : lb;
int msb = 0;
while ((ulb << msb) < ula) msb++;
int q = 0;
for (int i = msb; i >= 0; i--) {
if ((ulb << i) > ula) continue;
... 阅读全帖
l**b
发帖数: 457
21
来自主题: JobHunting版 - Divide Two Integers
public int divide(int dividend, int divisor) {
assert(divisor != 0);
if (dividend == 0) {
return 0;
}

boolean negative = false;
if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)) {
negative = true;
}

int result = 0;
long lDividend = Math.abs((long) dividend);
long lDivisor = Math.abs((long) divisor);
while (lDividend >= lDivisor) {
long d = lDiv... 阅读全帖
t******l
发帖数: 10908
22
来自主题: Parenting版 - 该不该离开酱油游泳队
你这个是动 divisor 的搞法,确实更快一点,我写个比较严谨
的式子就是:
2*83*7/49 => 2*83/7 => 2*(84-1)/7 => (-2)/7 => 5/7 =>
(5*7)/(7*7) => 35/49
不过纯从教育角度讨论,动 divisor 情况下的同余循环,对普通娃
入门而言,更 confusing 一些。。。我觉得先掌握 divisor 不动,
依赖 distributive property 的办法,相对比较直观。。。掌握
后再进阶到动 divisor,这样普通娃比较容易接受。。。个人看法
。。。
话说回来,其实我自我感觉,我自己动 divisor 的出错率更高。。。
s***m
发帖数: 6197
23
来自主题: Stock版 - 死皮换股是怎么操作的呀
可能会影响将来的走势
但是不会影响当前的价值
以下是从wiki拷贝过来的
Index maintenance[edit]
In order to keep the S&P 500 Index consistent over time, it is adjusted to c
apture corporate actions which affect market capitalization, such as additio
nal share issuance, dividends and restructuring events such as mergers or sp
in-offs. Additionally, to remain indicative of the U.S. stock market, the co
nstituent stocks are changed from time to time.[3]
To prevent the value of the Index from changing merely as a result of corpor
ate... 阅读全帖
F********g
发帖数: 475
24
unsigned int calculate_1X_LO_period(unsigned int input_freq_khz, unsigned
int if_freq_khz)
{
unsigned int
remainder;
unsigned int temp_result;
unsigned int divisor;

DISABLE_TMR1_INT;
DISABLE_TMR4_INT;
DISABLE_SPI1_INT;
dividend.all=32000;
divisor=input_freq_khz+if_freq_khz;
if (0==divisor)
... 阅读全帖
p*****2
发帖数: 21240
25
来自主题: JobHunting版 - 关于除法的问题
int divide(int dividend, int divisor) {
if (divisor == 0)
throw new ArithmeticException();
int k = 0;
long a = dividend;
long b = divisor;
a = Math.abs(a);
b = Math.abs(b);
boolean neg = (dividend & 1 << 31 ^ divisor & 1 << 31) != 0;
while (b << k <= a)
k++;
int r = 0;
for (int i = k; i >= 0; i--) {
if (b << i > a)
continue;
else {
r |= 1 << i;
a -= b << i;
}
}
return neg ? -r : r;
... 阅读全帖
z****e
发帖数: 54598
26
来自主题: JobHunting版 - leetcode: Divide Two Integers 怎么做?
public class Solution {
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function
if(dividend == 0) return 0;
long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor);
long[] result = div(a,b);
long r = ((dividend>0&&divisor<0)||(dividend<0&&divisor>0))?-result[
0]:result[0];
return (int)r;
}
private long[] div(long a, long b){
l... 阅读全帖
m*******s
发帖数: 23
27
来自主题: JobHunting版 - M 家电面
public int divide(int dividend, int divisor) {
if (divisor == 0) throw new IllegalArgumentException();

long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor);
int result = 0;
while (a >= b) {
long c = b;
for (int i = 0; c <= a; i++) {
a -= c;
result += 1 << i;
c <<= 1;
}
}
return ((dividend^divisor) >> 31) == 0 ? result : 0 - result;
... 阅读全帖
t*******r
发帖数: 22634
28
其实我发现我的 973 楼写错了,因为看着我们那个说法写的。应该看 wiki
的那个写就不会错了。wiki 里面有那个 if and only if,我没写出来。
我写出来的只有 if,漏了 only if,所以其实错了。
不过我们也可以将错就错先跑一下。
=============================================
比如你给 973 楼虚拟 Yacc 一个输入 (2, 3, 5)
input:(2, 3, 5)
shift: 5 divisor (2, 3)
reduce: (2, 3)
shift: 3 divisor (2)
reduce: (2)
SUCCESS
=============================================
如果给 input (2, 4, 5)
input: (2, 4, 5)
shift: 5 divisor (2, 4)
reduce: (2, 4)
shift: 4 divisor (2) !!! FAIL
FAIL
=======================================... 阅读全帖
a*********3
发帖数: 660
29
定义 definition变量 variable面积 area直径 diameter半径 radius公式 formula
单价 unit price范围 range/scope/extent集合 set法则 principle本金 principal利
率 interest rate利息 interest单利 simple interest复利 compound interest正数
positive number负数 negative number解析式 analytic expression分类讨论
classified discussion性质 nature (不是很确定)奇函数 odd function偶函数
even function对称 symmetric坐标原点 origin单调性 monotonicity(不是很确定)
任意 random周期性 periodic 有界性 boundedness 数学 mathematics, maths(BrE)
, math(AmE) 公理 axiom 定理 theorem 计算 calculation 运算 operat... 阅读全帖
c******t
发帖数: 1500
30
来自主题: JobHunting版 - 这道题,怎么做呀?
用减法
int divide(int target, int divisor)
{
int result = 0;
while (target - divisor > 0)
{
target -= divisor;
++result;
}
return result;
}
r*******g
发帖数: 1335
31
来自主题: JobHunting版 - Amazon interview question.
where did you get this O((logn)^4)?
testing whether primary might be an open problem. Even if it takes O((logn)^
4), that means after O((logn)^4) it is still primary, then, we already
ignore those small primaries which are not its divisor. We can still try to
find its primary divisor first. In this way, it is still quicker than
directly test sqrt(n) numbers.
In reality, primary numbers are known for some range. Find the primary
divisors should work fast. Otherwise, testing whether it is primary ... 阅读全帖
h******i
发帖数: 30
32
来自主题: JobHunting版 - 关于除法的问题
照着写了个code 不过放在leetcode上测试连small set都超时了。那位给贴个效率高的
,让我学习一下。
int Multiply(int x, int y)
{
bool neg = (x>0&&y<0)||(x<0&&y>0);
y = abs(y);
int res = 0;
int orgy = y;
while(y>1)
{
res += x << 1;
y-=2;
}
if(y==1)
res+=x;
if(neg&&orgy<0)
return -res;
else
return res;
}
int divide(int dividend, int divisor) {
int res = dividend;
int step = dividend;
while(1)
{
int product = Multiply(res,divisor);
... 阅读全帖
n****r
发帖数: 471
33
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
用c++实现的,在leetcode上一跑就超时。
卡在
dividend = 2147483647
divisor = 1
Code 是:
int divide(int dividend, int divisor){
long a = dividend;
long b = divisor;
bool neg = false;
if(a < 0)
neg = !neg;
if(b < 0)
neg = !neg;
a = abs(a);
b = abs(b);
int d = 0;
while(b< d++;
int c = 0;
for(int i = d; i >=0; i--){
if(b< c |= 1 << i;
a -= b < }
}
if(neg)
c = -c;
return c;
}
n****r
发帖数: 471
34
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
用c++实现的,在leetcode上一跑就超时。
卡在
dividend = 2147483647
divisor = 1
Code 是:
int divide(int dividend, int divisor){
long a = dividend;
long b = divisor;
bool neg = false;
if(a < 0)
neg = !neg;
if(b < 0)
neg = !neg;
a = abs(a);
b = abs(b);
int d = 0;
while(b< d++;
int c = 0;
for(int i = d; i >=0; i--){
if(b< c |= 1 << i;
a -= b < }
}
if(neg)
c = -c;
return c;
}
p*****2
发帖数: 21240
35
来自主题: JobHunting版 - Divide Two Integers
常规题得多练呀。
public int divide(int dividend, int divisor)
{
if (divisor == 0)
throw new ArithmeticException();
long a = dividend;
long b = divisor;

boolean neg = false;
if (a < 0)
neg = !neg;
if (b < 0)
neg = !neg;
a=Math.abs(a);
b=Math.abs(b);
int c = 0;
while (b << c <= a)
c++;
int ans = 0;
while (c >= 0)
{
if (b << c <=... 阅读全帖
p*****2
发帖数: 21240
36
来自主题: JobHunting版 - Divide Two Integers
常规题得多练呀。
public int divide(int dividend, int divisor)
{
if (divisor == 0)
throw new ArithmeticException();
long a = dividend;
long b = divisor;

boolean neg = false;
if (a < 0)
neg = !neg;
if (b < 0)
neg = !neg;
a=Math.abs(a);
b=Math.abs(b);
int c = 0;
while (b << c <= a)
c++;
int ans = 0;
while (c >= 0)
{
if (b << c <=... 阅读全帖
h**o
发帖数: 548
37
来自主题: JobHunting版 - leetcode: Divide Two Integers 怎么做?
题目: Divide two integers without using multiplication, division and mod
operator.
应该是怎么做哪?leetcode 有个人气解答如下,但是我看不懂, 有谁给解释一下你怎
么做的?
int divide(int dividend, int divisor) {
long long a = abs((double)dividend);;
long long b = abs((double)divisor);
long long ret = 0;
while (a >= b) {
long long c = b;
for (int i = 0; a >= c; ++i, c <<= 1) {
a -= c;
ret += 1 << i;
}
}
return ((dividen... 阅读全帖
l***o
发帖数: 7937
38
烦请转交163。谢谢
--------------------------
合数必被某一素数整除
合数的定义:除了1和它本身之外还有别的divisor.
如果这个divisor是素数,done; 如果这个divisor还是合数,repeat.
t******n
发帖数: 2939
39
☆─────────────────────────────────────☆
firearasi (firearasi) 于 (Thu May 23 18:10:13 2013, 美东) 提到:
1. 假设p1=2,p2=3,...,pn 是全部的素数...
2. 令 N=p1*p2*...*pn+1, 显然 N比 p1,p2,...,pn都大,因此不再p1,p2,..,pn,中, 所
以根据1, N是一个合数.
3. N是合数,那么必然能够被一个素数整除, 根据1, 所有的素数是p1,..., pn, 所以必
然有其中之一, 比如pj, 能整除N, 特别的, N 除以pj余数为0
4. N= pj*(p2*...*pn 括号内排除pj)+1=pj*something+1, 因此, N除以pj余数为1
5. 3 和 4 矛盾,N不能同时除以pj余0,而且余1.
现在有几种推理方法
6(I), 3和4矛盾, 矛盾的根源在于 假设1 是完全错误的, 于是 证毕,素数无穷 Q.E.D
或者走下列路线(l63路线)
6(II), 3和4矛盾的一个最近的根源来自于第3部的N是合数, ... 阅读全帖
d******s
发帖数: 180
40
来自主题: Mathematics版 - Goldston撰文介绍老张的工作
http://aimath.org/news/primegaps70m/
第一段就很有料,四月后期投稿,五月中旬便接受,对于一篇55页的paper来说速度惊
人。
Zhang's Theorem on Bounded Gaps Between Primes
by Dan Goldston
In late April 2013 Yitang Zhang of the University of New Hampshire submitted
a paper to the Annals of Mathematics proving that there are infinitely many
pairs of primes that differ by less than 70 million. The proof of this
amazing result was verified with high confidence by several experts in the
field and accepted for publication. A public slightly re... 阅读全帖
l***o
发帖数: 7937
41
合数必被某一素数整除
合数的定义:除了1和它本身之外还有别的divisor.
如果这个divisor是素数,done; 如果这个divisor还是合数,repeat.
m*******r
发帖数: 7495
42
来自主题: Statistics版 - 问个R的小题目
这个问题的b)问
R language if(), while() etc
A positive integer number n is a prime number if it is only divisible
by 1 and itself.
a)Use it to write checkDiv <- function(n,k) {........} which either
writes " is divisible by " or " / has remainer
" when it is not divisible. Hint: use paste()
b) Now use a loop, searching for possible divisors of n to write a function
is.primeNum <- function(n) { .... } which returns true whenever n is a prime
number.
下面附上了原题的答案,我想用另外一个办法,不过本人超级菜鸟,把自己绕进... 阅读全帖
j*******l
发帖数: 1066
43
int Div(int a, int b) {
try{
if (b == 0) {
throw runtime_error("divisor is 0");
}
if( (a>0&&b<0) || (a>0&&b<0) ){
int sign = -1;
if(a<0){
a= Mul(-1,a);
}
if(b<0){
b= Mul(-1,b);
}
return Mul(sign, Div(a,b));
}
if(a<0){
a= Mul(-1,a);
}
if(b<0){
b= Mul(-1,b);
}
return (a阅读全帖
j*******l
发帖数: 1066
44
int Div(int a, int b) {
try{
if (b == 0) {
throw runtime_error("divisor is 0");
}
if( (a>0&&b<0) || (a>0&&b<0) ){
int sign = -1;
if(a<0){
a= Mul(-1,a);
}
if(b<0){
b= Mul(-1,b);
}
return Mul(sign, Div(a,b));
}
if(a<0){
a= Mul(-1,a);
}
if(b<0){
b= Mul(-1,b);
}
return (a阅读全帖
p*****2
发帖数: 21240
45

就是改成long了。
public int divide(int dividend, int divisor) // return c=a/b;
{
long a = dividend;
long b = divisor;
if (b == 0)
throw new ArithmeticException();
boolean neg = false;
if (a < 0)
neg = !neg;
if (b < 0)
neg = !neg;
a = Math.abs(a);
b = Math.abs(b);
int d = 0;
while (b << d <= a)
{
d++;
}
int c = 0;
for (int i = d; i >= 0... 阅读全帖
p*****2
发帖数: 21240
46
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
public int divide(int dividend, int divisor) // return c=a/b;
{
long a = dividend;
long b = divisor;
if (b == 0)
throw new ArithmeticException();
boolean neg = false;
if (a < 0)
neg = !neg;
if (b < 0)
neg = !neg;
a = Math.abs(a);
b = Math.abs(b);
int d = 0;
while (b << d <= a)
{
d++;
}
int c = 0;
for (int i = d; i >= 0; i--)
... 阅读全帖
p*****2
发帖数: 21240
47
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
public int divide(int dividend, int divisor) // return c=a/b;
{
long a = dividend;
long b = divisor;
if (b == 0)
throw new ArithmeticException();
boolean neg = false;
if (a < 0)
neg = !neg;
if (b < 0)
neg = !neg;
a = Math.abs(a);
b = Math.abs(b);
int d = 0;
while (b << d <= a)
{
d++;
}
int c = 0;
for (int i = d; i >= 0; i--)
... 阅读全帖
w****a
发帖数: 710
48
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
我想知道这题这么写,面试会算你过么??
我发誓我没用用"运算符"
int divide(int dividend, int divisor)
{
int result = 0;
__asm
{
mov eax,dividend
cdq
idiv divisor
mov result,eax
}

return result;
}
w****a
发帖数: 710
49
来自主题: JobHunting版 - 两个整数除法的问题太刁钻了吧
我想知道这题这么写,面试会算你过么??
我发誓我没用用"运算符"
int divide(int dividend, int divisor)
{
int result = 0;
__asm
{
mov eax,dividend
cdq
idiv divisor
mov result,eax
}

return result;
}
P******r
发帖数: 842
50
来自主题: JobHunting版 - Divide Two Integers
这题能用log exp做吗?能用的话code太短了。另外偷懒了,除0就返回0了。
int divide(int dividend, int divisor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
long long x = dividend;
long long y = divisor;
if (0 == x || 0 == y)
return 0;
int sign = (x > 0 && y > 0 || x < 0 && y < 0) ? 1 : -1;
x = abs(x);
y = abs(y);
int r = exp(log(x)-log(y));
return sign*r;
}
1 2 3 下页 末页 (共3页)