SHOW:
|
|
- or go back to the newest paste.
1 | package ch.claude_martin.playground; | |
2 | ||
3 | import java.util.Arrays; | |
4 | - | import java.util.stream.IntStream; |
4 | + | |
5 | public class SomeClass { | |
6 | ||
7 | - | /** Sum of all given points. */ |
7 | + | |
8 | - | static int sum(int[] points) { |
8 | + | |
9 | - | int result = 0; |
9 | + | final int n = 5; |
10 | - | for (int p : points) { |
10 | + | // initialized as [0,0,0,0,0], |
11 | - | result += p; |
11 | + | |
12 | final byte[] points = new byte[n]; | |
13 | - | return result; |
13 | + | // initially the sum is 0; |
14 | for (int sum = 0; sum < 3 * n; ) { | |
15 | // Get next combination: | |
16 | - | // alternative implementation: |
16 | + | for (int j = 0; true; ++j) { |
17 | - | static int sum2(int... points) { |
17 | + | if (points[j] == 3) { |
18 | - | return (int) IntStream.of(points).sum(); |
18 | + | |
19 | sum -= 3; | |
20 | } else { | |
21 | ++points[j]; | |
22 | ++sum; | |
23 | - | int n = 5; |
23 | + | |
24 | - | |
24 | + | |
25 | - | // initialized as [0,0,0,0,0], |
25 | + | |
26 | ||
27 | - | int[] points = new int[n]; |
27 | + | if (sum == n) // valid result? |
28 | System.out.println(Arrays.toString(points)); | |
29 | - | for (;;) { // endless loop |
29 | + | |
30 | } | |
31 | - | for (int j = 0; j < points.length; ++j) { |
31 | + |