如何不使用比较运算就可以求出两个数的最大值与最小值

package java程序员面试笔试宝典;

public class 题8_8_2不使用比较运算符求两个数的最大值最小值 {
	public static void main(String[] args) {
		System.out.println(Integer.MAX_VALUE);
		System.out.println(getMin(214, 2147483647));
	}
	public static int getMax(int a,int b){
		return (int)(((long)a+b+Math.abs(a-b))/2);
	}
	public static int getMin(int a,int b){
		return (int)(((long)a+b-Math.abs(a-b))/2);
	}
}

猜你喜欢

转载自blog.csdn.net/m0_38068868/article/details/81740024