given 2 unsigned ints a and b, return 1 unsigned int = a/b, rounded to nearest int without float operation
Respostas da entrevista
Sigiloso
4 de jul. de 2013
the round up operation is not useful to find the"nearest int"
you could use:
((a%b)>=(b-(a%b)))?(a/b)+1:(a/b)
4
Sigiloso
21 de fev. de 2015
return (int) ( ( a + (b>>1) ) / b ) ;
1
Sigiloso
28 de jan. de 2015
You can derive the answer:
in order to round up, you want REMINDER >= b/2
let's play: 2*REMINDER >= b
What is REMINDER??? 2*(a%b) >= b
So, if TRUE: answer is (a%b)+1
if FALSE, the answer is a%b