is this an error? I think 100579234/-555806774 is supposed to be -1, right? and I have tried it in python terminal
I have tested it, in python & cpp. They gave different result, -1 for python, 0 for cpp.
It might be problem of languages. For this problem, we should follow the cpp/java division principle.
I agree. But when I follow cpp rule. The same program gets different answer from OJ and my local machine: Input: -2147483648, -1109186033
Output: 0(OJ), 1(Local machine)
it's really weird.
From the ANSI C draft (3.3.5):
If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or
the smallest integer greater than the algebraic quotient is
implementation-defined, as is the sign of the result of the %
operator. If the quotient a/b is representable, the expression (a/b)*b
- a%b shall equal a.
Thus, different compilers can have different implementations and either way is correct. In a real interview, you should be fine to do however you want. And I assume interviewer will be pleased you can point out the potential ambiguous here. However, if someone says 5/-2 must equal to -2 without indicate the expected behaviour, he or she is not qualified to ask this question at all, and please don't bother joining the team.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.