题目大意:给一个数n,统计n的阶乘中各个数字出现的次数。用java的大数做。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner sc = new Scanner(System.in); 10 BigInteger[] fact = new BigInteger[370]; 11 fact[0] = BigInteger.valueOf(1); 12 for (int i = 1; i <= 366; i++) 13 fact[i] = fact[i-1].multiply(BigInteger.valueOf(i)); 14 int[] cnt = new int[10]; 15 int n; 16 while (sc.hasNext()) 17 { 18 n = sc.nextInt(); 19 if (n == 0) break; 20 BigInteger f = fact[n]; 21 for (int i = 0; i < 10; i++) cnt[i] = 0; 22 while (f.compareTo(BigInteger.ZERO) > 0) 23 { 24 BigInteger r = f.mod(BigInteger.valueOf(10)); 25 int idx = r.intValue(); 26 cnt[idx]++; 27 f = f.divide(BigInteger.valueOf(10)); 28 } 29 System.out.printf("%d! -- ", n); 30 System.out.printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d ", cnt[0], cnt[1], cnt[2], cnt[3], cnt[4]); 31 System.out.printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d ", cnt[5], cnt[6], cnt[7], cnt[8], cnt[9]); 32 } 33 } 34 }
一直不习惯java的输出,今天才发现c风格的printf函数,哈哈~