日期:2014-05-20 浏览次数:20715 次
BigInteger result = new BigInteger("1"); for(int i = 2;i < 1001;++i){ BigInteger temp = new BigInteger(String.valueOf(i)); result = temp.multiply(result); } String s = result.toString(); int count = 0; for(int i = 0;i < s.length();++i){ if(s.charAt(i) == '2'){ ++count; } } System.out.println(count);
------解决方案--------------------
public class Test { public static void main(String[] args) { System.out.println(deal()); } public static String factorial(int n) { //求阶乘 BigDecimal ans = new BigDecimal(1); for (int i = 1; i <= n; i++ ) { ans = ans.multiply(new BigDecimal(i)); } return ans.toPlainString(); } public static int count(String s,char x) { //差数 int result = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == x) { result++; } } return result; } public static int deal(){ return count(factorial(1000),'2'); } }