------
Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
由一道黑马基础测试题引入该问题:
求1000!的结果中包含多少个0?注:1000! =1×2×3×4×5×...×999×1000
分析:这题很明显超出了普通的 int 和long的表示范围,从而想到了使用String来接收计算结果然后再进行拆分得出结果但是这样处理起来太麻烦了。java中专门有BigInteger和BigDecimal两个类来进行大数的处理。
一.BigInteger
BigInteger中封装了很多的方法,下面是它的常用的方法:
package com.itheima;
import java.math.BigInteger;
public class test {
public static void main(String[] args) {
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("987654321");
System.out.println("加法操作:"+big2.add(big1));
System.out.println("减法操作:"+big2.subtract(big1));
System.out.println("乘法操作:"+big2.multiply(big1));
System.out.println("除法操作:"+big2.divide(big1));
}
}
输出:
加法操作:1111111110
减法操作:864197532
乘法操作:121932631112635269
除法操作:8
二.BigDecimal
上面的BigIntegr不能进行精准的计算如果要进行精准的计算则需要使用BigDecimal
import java.math.* ;
class MyMath{
public static double add(double d1,double d2){ // 进行加法计算
BigDecimal b1 = new BigDecimal(d1) ;
BigDecimal b2 = new BigDecimal(d2) ;
return b1.add(b2).doubleValue() ;
}
public static double sub(double d1,double d2){ // 进行减法计算
BigDecimal b1 = new BigDecimal(d1) ;
BigDecimal b2 = new BigDecimal(d2) ;
return b1.subtract(b2).doubleValue() ;
}
public static double mul(double d1,double d2){ // 进行乘法计算
BigDecimal b1 = new BigDecimal(d1) ;
BigDecimal b2 = new BigDecimal(d2) ;
return b1.multiply(b2).doubleValue() ;
}
public static double div(double d1,double d2,int len){ // 进行乘法计算
BigDecimal b1 = new BigDecimal(d1) ;
BigDecimal b2 = new BigDecimal(d2) ;
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
}
public static double round(double d,int len){ // 进行四舍五入
BigDecimal b1 = new BigDecimal(d) ;
BigDecimal b2 = new BigDecimal(1) ;
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
}
};
public class BigDecimalDemo01{
public static void main(String args[]){
System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345,3.333),1)) ;
System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345,3.333),3)) ;
System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345,3.333),2)) ;
System.out.println("除法运算:" + MyMath.div(10.345,3.333,3)) ;
}
};
三.开始处题目的代码
package com.itheima;
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 9、 求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000
*
* 思路:1.使用java中的处理大数的类将阶乘计算的结果转换成字符串的形式
* 2.然后使用正则表达式将字符串中的0的个数匹配出来
* @author xiajie
*
*/
public class Test9 {
public static void main(String[] args) {
System.out.println(zeroNum(jiecheng(1000)));
}
//求出阶乘,以字符串形式返回结果
public static String jiecheng(int x){
BigInteger result = new BigInteger("1");
for(int i = 1;i<=x;i++){
result = result.multiply(new BigInteger(String.valueOf(i)));
}
return result.toString();
}
//用来根据计算后的字符串求出有多少个0
public static int zeroNum(String string){
int count = 0;
String regex = "";//正则表达是式的形式规则
Pattern parttern = Pattern.compile(regex);//实例化Pattern对象
Matcher matcher = parttern.matcher(string);//实例化Matcher对象
//根据正则的的规则来查找字符串中符合的字符
while(matcher.find()){
count++;
}
return count;
}
}