您的位置:首页 > 其它

求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000(求阶乘)

2015-05-28 12:24 344 查看
求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000(求阶乘)

package yang.base;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Test33 {
public static void main(String[] args) {
//定义开始时间
long start= System.currentTimeMillis();
//创建开始对象初值为1
BigInteger bi = new BigInteger("1");
//		创建结束对象初值为1000
BigInteger bi2 = new BigInteger("1000");
//		创建求和对象
BigInteger sum = new BigInteger("1");
//		记录个数
int count = 0;
//		求出1-1000的阶乘
for (int x = bi.intValue(); x <= bi2.intValue(); x++) {
//			使用BigInteger中的valueOf方法把整数类型的数据转换成BigInteger类型的数据,并相乘
sum = sum.multiply(BigInteger.valueOf(x));
}
//先把这个结果转换成string类型
String s= sum.toString();
//再使用string类型转换成char类型
char[] result=s.toCharArray();
//统计个数
for(int x=0;x<result.length;x++){
if(result[x]=='0'){
count++;
}
}
System.out.println("1--1000的阶乘中一共有:"+count+"个零");
long end=System.currentTimeMillis();
System.out.println("共耗时:"+(end-start));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: