M面经Prepare: Find integer Average of 2 integers. - neverlandly - 博客园
-9%2 == -1 不是1, 所以负数%2==1不是奇数判定标准。但是偶数标准仍是%2 == 0
The definition of integer average is the highest smaller integer if average is
floating point number. Also the condition if that they can not use any typecasting
or any datatype other than int.
Example: a = 4, b = 5, avg = 4 a = 4, b = 6, avg = 5 a = -4, b = -6, avg = -5
a = 4, b = -5, avg = -1 a = -4, b = -5, avg = -5
4 public int aver(int a, int b) { 5 if ((a+b)%2 == 0)return (int)(a+b)/2; 6 else return (int)(a+b-1)/2; 7 }Read full article from M面经Prepare: Find integer Average of 2 integers. - neverlandly - 博客园