Advertisement
DulcetAirman

basketball

May 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package ch.claude_martin.playground;
  2.  
  3. import java.util.Arrays;
  4. import java.util.stream.IntStream;
  5.  
  6. public class SomeClass {
  7.     /** Sum of all given points. */
  8.     static int sum(int[] points) {
  9.         int result = 0;
  10.         for (int p : points) {
  11.             result += p;
  12.         }
  13.         return result;
  14.     }
  15.  
  16.     // alternative implementation:
  17.     static int sum2(int... points) {
  18.         return (int) IntStream.of(points).sum();
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.         // number of tries:
  23.         int n = 5;
  24.        
  25.         // initialized as [0,0,0,0,0],
  26.         // which is not a valid result
  27.         int[] points = new int[n];
  28.  
  29.         for (;;) { // endless loop
  30.             // Get next combination:
  31.             for (int j = 0; j < points.length; ++j) {
  32.                 if (points[j] == 3)
  33.                     points[j] = 0;
  34.                 else {
  35.                     ++points[j];
  36.                     break;
  37.                 }
  38.             }
  39.  
  40.             int sum = sum(points);
  41.  
  42.             if (sum(points) == n) // valid result?
  43.                 System.out.println(Arrays.toString(points));
  44.             if (sum == 3 * n) // stop criterion
  45.                 break;
  46.  
  47.         }
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement