# Moderate 不用比较符返回较大的数 @CareerCup

1290人阅读 评论(0)

if a and b have different signs:
//  if a > 0, then b <  <d,  and  k = l.
// if a < 0, then b > 0, and k = 0.
// so either way, k = sign(a)
let k = sign(a)
else
let  k  = sign(a  -  b)  //  overflow  is  impossible

int k = use_sign_of_a * sa + use_sign_of_c * sc;

package Moderate;

/**
* Write a method which finds the maximum of two numbers. You should not use if-else or any other comparison operator.

EXAMPLE

Input: 5, 10

Output: 10

*
*/
public class S17_4 {

/* Flips a 1 to a 0 and a 0 to a 1 */
public static int flip(int bit) {
return 1 ^ bit;
}

/* Returns 1 if a is positive, and 0 if a is negative */
public static int sign(int a) {
return flip((a >> 31) & 0x1);
}

public static int getMaxNaive(int a, int b) {
int k = sign(a - b);
int q = flip(k);
return a * k + b * q;
}

public static int getMax(int a, int b) {
int c = a - b;

int sa = sign(a); 		// if a >= 0, then 1 else 0
int sb = sign(b); 		// if b >= 0, then 1 else 0
int sc = sign(c);		// depends on whether or not a - b overflows

/* We want to define a value k which is 1 if a > b and 0 if a < b.
* (if a = b, it doesn't matter what value k is) */

int use_sign_of_a = sa ^ sb; 		// If a and b have different signs, then k = sign(a)
int use_sign_of_c = flip(sa ^ sb); 	// If a and b have the same sign, then k = sign(a - b)

/* We can't use a comparison operator, but we can multiply values by 1 or 0 */
int k = use_sign_of_a * sa + use_sign_of_c * sc;
int q = flip(k); // opposite of k

return a * k + b * q;
}

public static void main(String[] args) {
int a = 26;
int b = -15;

System.out.println("max_naive(" + a + ", " + b + ") = " + getMaxNaive(a, b));
System.out.println("max(" + a + ", " + b + ") = " + getMax(a, b));

a = -15;
b = 2147483647;

System.out.println("max_naive(" + a + ", " + b + ") = " + getMaxNaive(a, b));
System.out.println("max(" + a + ", " + b + ") = " + getMax(a, b));
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
博客专栏
 CareerCup专栏 文章：41篇 阅读：59300
 LeetCode专栏 文章：148篇 阅读：562602
阅读排行
个人资料
• 访问：1221161次
• 积分：16552
• 等级：
• 排名：第606名
• 原创：407篇
• 转载：64篇
• 译文：0篇
• 评论：290条