package cn.edu.henu.test.mytest;
/**
* Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API. <br>
* 1、不得使用任何API,如Math.abs()等。<br>
* 2、不得使用判断语句,如if、for、while、switch、?:等。<br>
* 3、不得使用比较语句,如:==、 <=、>=、!=、 <、>等。 <br>
*/
public class LessionSystemArraycopy {
public static void main(String[] args) {
for (int i = -1000; i <= 1000; i++) {
System.out.println(abs(i));
}
}
public static int abs(int num) {
return num * (1 - ((num >>> 31)<<1));
}
}
如何得到一个整数的绝对值
最新推荐文章于 2023-07-30 18:29:32 发布