java 常见数学方法

(max)(min)(abs)(sqrt)

int max = Math.max(1, 2);      最大值
int min = Math.min(1, 2);      最小值
int abs = Math.abs(1);         绝对值
double sqrt = Math.sqrt(1);    开平方

取绝对值

Java位运算 常见的位运算

(floor)(ceil)(round)(rint)

-----------------------------------------------------------------------------
# Math.floor → 取整取小 (地板)
-----------------------------------------------------------------------------
System.out.println(Math.floor(1.9));  // 1.0
System.out.println(Math.floor(1.6));  // 1.0
System.out.println(Math.floor(1.5));  // 1.0
System.out.println(Math.floor(1.2));  // 1.0
System.out.println(Math.floor(0.9));  // 0.0
System.out.println(Math.floor(0.5));  // 0.0
System.out.println(Math.floor(0.2));  // 0.0
System.out.println(Math.floor(0));    // 0.0
System.out.println(Math.floor(-0.2)); // -1.0
System.out.println(Math.floor(-0.5)); // -1.0
System.out.println(Math.floor(-0.9)); // -1.0
System.out.println(Math.floor(-1.2)); // -2.0
System.out.println(Math.floor(-1.5)); // -2.0
System.out.println(Math.floor(-1.6)); // -2.0
System.out.println(Math.floor(-1.9)); // -2.0
-----------------------------------------------------------------------------
# Math.ceil → 取整取大(天花板)
-----------------------------------------------------------------------------
System.out.println(Math.ceil(1.9));   // 2.0
System.out.println(Math.ceil(1.6));   // 2.0
System.out.println(Math.ceil(1.5));   // 2.0
System.out.println(Math.ceil(1.2));   // 2.0
System.out.println(Math.ceil(0.9));   // 1.0
System.out.println(Math.ceil(0.5));   // 1.0
System.out.println(Math.ceil(0.2));   // 1.0
System.out.println(Math.ceil(0));     // 0.0
System.out.println(Math.ceil(-0.2));  // 0.0
System.out.println(Math.ceil(-0.5));  // 0.0
System.out.println(Math.ceil(-0.9));  // 0.0
System.out.println(Math.ceil(-1.2));  // -1.0
System.out.println(Math.ceil(-1.5));  // -1.0
System.out.println(Math.ceil(-1.6));  // -1.0
System.out.println(Math.ceil(-1.9));  // -1.0
-----------------------------------------------------------------------------
# Math.round → 四舍五入 (0.5往大了进,1.5→2,-1.5→-1)
-----------------------------------------------------------------------------
System.out.println(Math.round(1.9));  // 2
System.out.println(Math.round(1.6));  // 2
System.out.println(Math.round(1.5));  // 2
System.out.println(Math.round(1.2));  // 1
System.out.println(Math.round(0.9));  // 1
System.out.println(Math.round(0.6));  // 1
System.out.println(Math.round(0.5));  // 1
System.out.println(Math.round(0.2));  // 0
System.out.println(Math.round(0));    // 0
System.out.println(Math.round(-0.2)); // 0
System.out.println(Math.round(-0.5)); // 0
System.out.println(Math.round(-0.6)); // -1
System.out.println(Math.round(-0.9)); // -1
System.out.println(Math.round(-1.2)); // -1
System.out.println(Math.round(-1.5)); // -1
-----------------------------------------------------------------------------
# Math.rint → 四舍五入 (0.5往偶数进,1.5→2,-1.5→-2)
-----------------------------------------------------------------------------
System.out.println(Math.rint(3.5));   // 4.0
System.out.println(Math.rint(2.5));   // 2.0
System.out.println(Math.rint(1.9));   // 2.0
System.out.println(Math.rint(1.6));   // 2.0
System.out.println(Math.rint(1.5));   // 2.0
System.out.println(Math.rint(1.2));   // 1.0
System.out.println(Math.rint(0.9));   // 1.0
System.out.println(Math.rint(0.6));   // 1.0
System.out.println(Math.rint(0.5));   // 0.0
System.out.println(Math.rint(0.2));   // 0.0
System.out.println(Math.rint(0));     // 0.0
System.out.println(Math.rint(-0.2));  // 0.0
System.out.println(Math.rint(-0.5));  // 0.0
System.out.println(Math.rint(-1.5));  // -2.0
System.out.println(Math.rint(-2.5));  // -2.0
System.out.println(Math.rint(-3.5));  // -4.0

强制类型转换

-----------------------------------------------------------------------------
# float强转为int(直接舍弃小数位)
-----------------------------------------------------------------------------
System.out.println((int) 3.0f); // 3
System.out.println((int) 3.1f); // 3
System.out.println((int) 3.2f); // 3
System.out.println((int) 3.3f); // 3
System.out.println((int) 3.4f); // 3
System.out.println((int) 3.5f); // 3
System.out.println((int) 3.6f); // 3
System.out.println((int) 3.7f); // 3
System.out.println((int) 3.8f); // 3
System.out.println((int) 3.9f); // 3
-----------------------------------------------------------------------------
# double强转为int(直接舍弃小数位)
-----------------------------------------------------------------------------
System.out.println((int) 3.0d); // 3
System.out.println((int) 3.1d); // 3
System.out.println((int) 3.2d); // 3
System.out.println((int) 3.3d); // 3
System.out.println((int) 3.4d); // 3
System.out.println((int) 3.5d); // 3
System.out.println((int) 3.6d); // 3
System.out.println((int) 3.7d); // 3
System.out.println((int) 3.8d); // 3
System.out.println((int) 3.9d); // 3
-----------------------------------------------------------------------------
# Integer/Integer(直接舍弃小数位)
-----------------------------------------------------------------------------
4/1 // 4
4/2 // 2
4/3 // 1
4/4 // 1
4/5 // 0
4/6 // 0
4/7 // 0
4/8 // 0
4/9 // 0
-----------------------------------------------------------------------------
# float/Integer(保留小数)
-----------------------------------------------------------------------------
4f/1 // 4.0
4f/2 // 2.0
4f/3 // 1.3333334
4f/4 // 1.0
4f/5 // 0.8
4f/6 // 0.6666667
4f/7 // 0.5714286
4f/8 // 0.5
4f/9 // 0.44444445

4.0f/1 // 4.0
4.0f/2 // 2.0
4.0f/3 // 1.3333334
4.0f/4 // 1.0
4.0f/5 // 0.8
4.0f/6 // 0.6666667
4.0f/7 // 0.5714286
4.0f/8 // 0.5
4.0f/9 // 0.44444445
-----------------------------------------------------------------------------
# double/Integer(保留小数)
-----------------------------------------------------------------------------
4.0d/1 // 4.0
4.0d/2 // 2.0
4.0d/3 // 1.3333333333333333
4.0d/4 // 1.0
4.0d/5 // 0.8
4.0d/6 // 0.6666666666666666
4.0d/7 // 0.5714285714285714
4.0d/8 // 0.5
4.0d/9 // 0.4444444444444444
-----------------------------------------------------------------------------
# 4.0/Integer(保留小数)(此时4.0默认为double)
-----------------------------------------------------------------------------
4.0/1 // 4.0
4.0/2 // 2.0
4.0/3 // 1.3333333333333333
4.0/4 // 1.0
4.0/5 // 0.8
4.0/6 // 0.6666666666666666
4.0/7 // 0.5714285714285714
4.0/8 // 0.5
4.0/9 // 0.4444444444444444

余数

-----------------------------------------------------------------------------
# 正数余数
-----------------------------------------------------------------------------
10%10 // 0
11%10 // 1
12%10 // 2
13%10 // 3
14%10 // 4
15%10 // 5
16%10 // 6
17%10 // 7
18%10 // 8
19%10 // 9
20%10 // 0
-----------------------------------------------------------------------------
# 负数余数
-----------------------------------------------------------------------------
-10%10 //  0 
-11%10 // -1
-12%10 // -2
-13%10 // -3
-14%10 // -4
-15%10 // -5
-16%10 // -6
-17%10 // -7
-18%10 // -8
-19%10 // -9
-20%10 //  0

涉及思想

NC100 字符串转换成整数
HJ70 矩阵乘法计算量估算


Java8

归约和汇总:(count) (maxmin)(summingIntsummingLongsummingDoublesum)(averagingIntaveragingLongaveragingIntaverage)(summarizingIntsummarizingLongsummarizingDouble)(joining)(reducing)
分组(groupingBy+countingsummingIntaveragingIntmaxByminBymapping)
分区(partitioningBy)


最大公约数、最小公倍数

# 计算一系列数字的最大公约数(gcd)
public static OptionalInt gcd(int[] numbers) {
    return Arrays.stream(numbers).reduce((a, b) -> gcd(a, b));
}
private static int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

# 计算数字数组的最低公共倍数(LCM)
public static OptionalInt lcm(int[] numbers) {
    IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y);
    return Arrays.stream(numbers).reduce((a, b) -> lcm.applyAsInt(a, b));
}

判断奇数,偶数

与运算(&)的用途

判断一个整数是否为2的整数次方

与运算(&)的用途

生成随机数

# 生成一个介于 Integer.MIN_VALUE 和 Integer.MAX_VALUE 之间的随机数。
public static int generateRandomInt() {
    return ThreadLocalRandom.current().nextInt();
}

判断为质数

public boolean isPrime(int candidate) {
    return IntStream.range(2, candidate).noneMatch(i -> candidate % i == 0);
}

# 对如上方法进行优化
public boolean isPrime(int candidate) {
    return IntStream.rangeClosed(2,  (int) Math.sqrt(candidate)).noneMatch(i -> candidate % i == 0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值