given 2 unsigned ints a and b, return 1 unsigned int = a/b, rounded to nearest int without float operation
Risposte di colloquio
Anonimo
4 lug 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
Anonimo
21 feb 2015
return (int) ( ( a + (b>>1) ) / b ) ;
1
Anonimo
28 gen 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