Sollicitatievraag bij Amazon

Given two numbers n and m, divide n by m without using the division operator. Return both the integer answer as well as the remainder.

Antwoorden op sollicitatievragen

Anoniem

30 jun 2011

I first assumed that both numbers were positive and then used repeated subtraction to come up with the answer. Upon further discussion I felt that this took too long and tried to come up with some kind of faster method to accomplish this but I could not design it in the time required. Thinking about it there are some other choices: 1) Treat the numbers as binary and then use shifting and subtraction to divide. I can't really solve this easily. 2) Calculate ..., 16m, 8m, 4m, 2m, 1m, etc. and the subtract each of those from n if possible. If you can subtract it shift the answer and add a 1. Finally, compare the signs of n and m to set the signs of the answer.

1

Anoniem

28 okt 2013

10 ^ (log10 m - log10 n)

Anoniem

8 apr 2014

The two answers don't give you the remainder; you will still have to write code to get that

Anoniem

8 apr 2014

Console.WriteLine("Enter a number n"); int n = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter a number m"); int m = Int32.Parse(Console.ReadLine()); int divisor = 0; int remainder = 0; int multiplier = 1; if (n < m) { Console.WriteLine("Divisor is 0"); Console.WriteLine("Remainder is {0}", m); } else { while (m * multiplier < n) { multiplier += 1; } divisor = multiplier - 1; remainder = n - m * (multiplier - 1); Console.WriteLine("Divisor is {0}", divisor); Console.WriteLine("Remainder is {0}", remainder); }

Anoniem

11 jul 2012

n * m ^(-1)