public class Bigger {
public static void main(String args[]){
int a = -2147483648;
int b = 2147483647;
String[] strArray = {"a>=b", "a<b"};
int i = (int)((long)a-(long)b >>> 63);
System.out.println(strArray[i]);
}
}
然后我在回帖中看到了几个很典型的方法(注意:不用比较运算符,不用类库方法。)
Java code
System.out.println(a-b); //无语,那还不如int a=1; int b=2;直接拿眼睛看算了。
Java code
if(!(a-b))
{
if(abs(a-b)-(a-b))
{
return "a < b";
}
else
{
return "a > b";
}
}
else
{
return "a = b";
}//方法很好,数学里整数能到无穷大,但是计算机里不行啊。
Java code
public String compare(Integer x,Integer y){
String [] buf={"=>"," <"};
int id1=(x-y)>>>31;
System.out.println(id1);
return buf[id1];
}//也是没有考虑越界问题
import java.util.Random;
/**
* 不通过比较运算和类库,比较两个 integer 大小
*/
public class SimpleCompare {
public static void main(String[] args) {
Random r = new Random();
int a = r.nextInt(), b = r.nextInt();
// int a = 33, b = 33;
String[] results = {"a < b", "a == b", "a > b"};
int sum = 1;
sum = sum - (int)((long)(a - b) >>> 63);
sum = sum + (int)((long)(b - a) >>> 63);
System.out.println(a + ", " + b + ": " + results[sum]);
}
}
------解决方案-------------------- up!!
------解决方案-------------------- 好帖子阿..又学习了.!UP
------解决方案--------------------