求1000!的结果中包含多少个0
1000! = 1×2×3×4×5×...×999×1000
为什么我算的结果是472个???
我看网上说的都是249个???why??
public class Test9 {
public static void main(String[] args) {
BigInteger big = BigInteger.valueOf(1);// 创建一个BigInteger类型
for (int i = 1; i <= 1000; i++) {// 循环计算1000!
big = big.multiply(BigInteger.valueOf(i));
}
String tmp = big.toString();// 将BigInteger 转换为String类型
int counter = 0;// 创建计数器
StringBuilder str = new StringBuilder();
for (int i = 0; i < tmp.length(); i++) {
if (tmp.charAt(i) == '0') {
str.append(tmp.charAt(i));
counter++;
}
}
System.out.println(str.length());//返回长度472
System.out.println(counter);//返回counter值472
System.out.println(tmp);//tmp的值
}
} |
|