M面经Prepare: Find integer Average of 2 integers.
of Find Integer 面经 prepare Integers average
2023-09-11 14:14:07 时间
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
1 package FindIntegerAverage; 2 3 public class Solution { 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 } 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Solution sol = new Solution(); 15 int res = sol.aver(-4, -5); 16 System.out.print(res); 17 } 18 19 }
相关文章
- Leetcode: Find Leaves of Binary Tree
- Leetcode: Lowest Common Ancestor of a Binary Search Tree
- comparison of truncate vs delete in mysql/sqlserver
- XV Open Cup named after E.V. Pankratiev. GP of Tatarstan
- .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.
- 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"
- JSON parse error: Cannot deserialize value of type `java.util.Date` from String 2023-02-19 00:00:00
- English Voice of <<Shallow>>
- linux下tar解压时报"gzip: stdin: unexpected end of file"如何处理?
- <SPECTRAL-SPATIAL CLUSTERING OF HYPERSPECTRAL IMAGE BASED ON LAPLACIAN REGULARIZED DEEP SUBSPACE >笔记
- DPC-Clustering by fast search and find of density peaks 2014
- Ant Design of Vue @1.7.8 学习笔记(Vue2版本)
- GDB错误:Cannot find bounds of current function
- hdu1595 find the longest of the shortest(Dijkstra)
- [LeetCode] 1295. Find Numbers with Even Number of Digits 统计位数为偶数的数字
- [LeetCode] 1022. Sum of Root To Leaf Binary Numbers 从根结点到叶结点的二进制数之和
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
- gzip: stdin: unexpected end of file tar: 归档文件中异常的 EOF
- 5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows