Input: 100579234, -555806774 Output: -1 Expected: 0


  • 0
    F

    is this an error? I think 100579234/-555806774 is supposed to be -1, right? and I have tried it in python terminal


  • 0
    S

    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.


  • 0
    F

    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.


  • 0
    O

    same here
    got different result from g++ and OJ


  • 1
    G

    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.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.