-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLotteryArray.java
40 lines (35 loc) · 1.05 KB
/
LotteryArray.java
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
package Lottery;
/**
* This program demonstrates a triangular array.
*
* @author Cay Horstmann
* @version 1.20 2004-02-10
*/
public class LotteryArray {
public static void main(String[] args) {
final int N_MAX = 10;
// allocate triangular array
int[][] odds = new int[N_MAX + 1][];
for (int n = 0; n <= N_MAX; n++)
odds[n] = new int[n + 1];
// fill triangular array
for (int n = 0; n < odds.length; n++) {
for (int k = 0; k < odds[n].length; k++) {
/*
* compute binomial coefficient n*(n-l)*(n-2)*...*(n-k+l)/(l*2*3*...*k)
*/
int lotteryOdds = 1;
for (int i = 1; i <= k; i++) {
lotteryOdds = lotteryOdds * (n - i + 1) / i;
}
odds[n][k] = lotteryOdds;
}
}
for (int[] row : odds) {
for (int odd : row) {
System.out.printf("%4d", odd);
}
System.out.println();
}
}
}