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. 数据库MySql阶段总结

    S1数据库中最重要的是查询,对于查询要有一个好的理解模型是很关键的: 1. 每一个查询都会返回一个结果集,这个结果集可能是一个值,一个字段或者一个记录,甚至可能是一个表 返回一个值 SELECT * ...

  2. Java笔试面试题二(常考问答)转

    1.说出ArrayList,Vector, LinkedList的存储性能和特性 ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允 ...

  3. Tab指示符——Indicator

    先说说我们的思路吧. 其实思路也很简单,就是在咱们的导航下面画一个小矩形,不断的改变这个矩形距离左边的位置. 思路就这么简单,有了思路,接下来就是实现了,看代码: public class Indic ...

  4. Squid 操作实践

    Squid简介 Squid可以做什么 性能要素 Squid安装 Squid快速体验 Squid配置 Squid简介 Squid is a caching proxy for the Web suppo ...

  5. 设计模式:观察者模式(Observer)

    定  义:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象.这个主题对象在状态发生 变化时,会通知所有观察者对象,使他们能够自动更新自己. 结构图: 抽象主题类: abstract c ...

  6. alibaba的FastJson(高性能JSON开发包)

    这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...

  7. 安装PHPStudy2014,打开端口出现80端口 PID4 进程:System-windows服务器应用

    原文:安装PHPStudy2014,打开端口出现80端口 PID4 进程:System-windows服务器应用-黑吧安全网 安装PHPStudy2014,打开端口出现80端口 PID4 进程:Sys ...

  8. SQL Server 2008 R2[ALTER]列属性修改

    1:向表中添加字段 Alter table [表名] add [列名] 类型 2:  删除字段 Alter table [表名]  drop column [列名] 3:  修改表中字段类型 (可以修 ...

  9. mysql导入导出

    1.导入整个库 进入数据库,source 进去的语句等同于直接连接数据库后数据的语句 >source /var/www/test.sql 或者 sy$ mysql -uroot -p 数据库名( ...

  10. MFC的简单加法器(二)

    创建对话框主要分两大步,第一,创建对话框资源,主要包括创建新的对话框模板.设置对话框属性和为对话框添加各种控件:第二,生成对话框类,主要包括新建对话框类.添加控件变量和控件的消息处理函数等.鸡啄米在本 ...