Array Division - What is the best way to divide two numbers stored in an array?

前端 未结 7 1178
名媛妹妹
名媛妹妹 2021-01-13 16:51

I have two arrays (dividend, divisor):

dividend[] = {1,2,0,9,8,7,5,6,6};
divisor[] = {9,8};

I need the result (dividend/divisor) as:

<
7条回答
  •  北海茫月
    2021-01-13 17:22

    Do long division.

    Have a temporary storage of size equal to the divisor plus one, and initialized to zero:

    accumulator[] = {0,0,0};
    

    Now run a loop:

    1. Shift each digit of the quotient one space to the left.
    2. Shift each digit of the accumulator one space to the right.
    3. Take the next digit of the dividend, starting from the most-significant end, and store it to the least-significant place of the accumulator.
    4. Figure out accumulator / divisor and set the least-significant place of the quotient to the result. Set the accumulator to the remainder.

    Used to use this same algorithm a lot in assembly language for CPUs what didn't have division instructions.

提交回复
热议问题