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

-9%2 == -1 不是1, 所以负数%2==1不是奇数判定标准。但是偶数标准仍是%2 == 0

 package FindIntegerAverage;

 public class Solution {
public int aver(int a, int b) {
if ((a+b)%2 == 0)return (int)(a+b)/2;
else return (int)(a+b-1)/2;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int res = sol.aver(-4, -5);
System.out.print(res);
} }

M面经Prepare: Find integer Average of 2 integers.的更多相关文章

  1. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  2. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  3. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  4. LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  5. Moving Average from Data Stream LT346

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. [leetcode]346. Moving Average from Data Stream滑动窗口平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  7. MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme

    错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named p ...

  8. 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计

    [抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...

  9. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

随机推荐

  1. cursor:pointer

    .cursor_hand{ cursor:pointer; }

  2. CentOS安装JAVA后JAVA版本不对的问题

    今天用CentOS安装JDK,发觉在安装完成后,输入java命令来验证是否安装成功时,出现 Usage: gij [OPTION] ... CLASS [ARGS] ...          to i ...

  3. Java Web项目调优原则

    1. 根据oracle生成的awr文件排除是否是数据库或者sql问题 2.配置中间件的dump文件路径,gc log文件路径 3.通过 MemoryAnalyzer 分析 dump文件 4.通过exc ...

  4. Sql Server建立链接服务器访问Access的MDB数据库

    EXEC master.dbo.sp_addlinkedserver @server = N'test', @srvproduct=N'OLE DB Provider for Jet', @provi ...

  5. 在Vista或更高版本Windows系统中, 获取超大图标的办法

    这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...

  6. Link Management Protocol (LMP)

    1.1. Link Management Protocol (LMP)   1.1.1.   Introduction and Theory The Link Manager (LM) transla ...

  7. RocEDU.阅读.写作

    RocEDU.阅读.写作 一.选择图书 <黑客大曝光> 二.读书计划 56天内学习完.时间:2016.01.25--2016.03.20 三.承诺 宋宸宁郑重承诺: 1.在56天内(开始时 ...

  8. WCF 无法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性

    无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式,或将 AspNetCompatibil ...

  9. zepto源码--filtered, contains,funcArg,setAttribute,className,deserializeVale--学习笔记

    几个方法 1.filtered 目标是对节点按照一定的选择器进行过滤. 如果传入了过滤选择器,则在nodes节点下,选择符合选择器的节点: 如果没有传入选择器,则返回节点本身,转化为zepto节点. ...

  10. boost::circular_buffer

    boost::circular_buffer的push_back分析 circular_buffer为了效率考虑,使用了连续内存块保存元素   使用固定内存,没有隐式或者非期望的内存分配 快速在cir ...