想了一下,不用数组和Collections应该是最快的。然后就写出了这个代码,分析后面有空补一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;

public class Test {

public static void main(String[] args){
int line = 0;
Scanner scanner = new Scanner(System.in);
line = scanner.nextInt();
scanner.close();
for( int i = 0 ; i <= line ; i ++){
for( int j = 0 ; j <= i ; j ++ ){
System.out.print(c(i,j)+" ");
}
System.out.println();
}
}

/**
* Calculate (m!)/(n!*(m-n)!)
* @param m The number of all factors
* @param n The number of the factors in a group
* @return result
*/
private static long c(int m,int n){
long mRes = 1;
long nRes = 1;
long mnRes = 1;
for( long i = 1 ; i <= m ; i ++){
mRes = mRes * i;
}
for( long i = 1 ; i <= n ; i ++){
nRes = nRes * i;
}
for( long i = 1 ; i <= (m-n) ; i ++){
mnRes = mnRes * i;
}
return mRes/(nRes*mnRes);
}


}