Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.claude_martin.playground;
- import java.util.Arrays;
- import java.util.stream.IntStream;
- public class SomeClass {
- /** Sum of all given points. */
- static int sum(int[] points) {
- int result = 0;
- for (int p : points) {
- result += p;
- }
- return result;
- }
- // alternative implementation:
- static int sum2(int... points) {
- return (int) IntStream.of(points).sum();
- }
- public static void main(String[] args) {
- // number of tries:
- int n = 5;
- // initialized as [0,0,0,0,0],
- // which is not a valid result
- int[] points = new int[n];
- for (;;) { // endless loop
- // Get next combination:
- for (int j = 0; j < points.length; ++j) {
- if (points[j] == 3)
- points[j] = 0;
- else {
- ++points[j];
- break;
- }
- }
- int sum = sum(points);
- if (sum(points) == n) // valid result?
- System.out.println(Arrays.toString(points));
- if (sum == 3 * n) // stop criterion
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement