Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. class GFG
  4. {
  5.     static int outcomes;
  6.    
  7.     static void printArray(int p[], int n, int teamSize)
  8.     {
  9.         int max = Integer.MIN_VALUE;
  10.         int maxCount = 0;
  11.         boolean badVal = false;
  12.        
  13.         if (n <= teamSize && !(p[0] == n - 1 && n == teamSize))
  14.         {
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 System.out.print(p[i]+" ");
  18.            
  19.                 if (p[i] == teamSize - 1)
  20.                 {
  21.                     maxCount++;
  22.                 }
  23.                 else if (p[i] >= teamSize)
  24.                 {
  25.                     badVal = true;
  26.                 }
  27.            
  28.                 max = Math.max(Integer.MIN_VALUE, p[i]);
  29.            
  30.             }
  31.  
  32.             if (badVal == false && n <= teamSize && maxCount <= 1)
  33.             {
  34.                 outcomes++;
  35.                 System.out.println("<- Added outcome");
  36.             }
  37.             else
  38.             {
  39.                 System.out.println();
  40.             }
  41.         }
  42.        
  43.     }
  44.      
  45.     // Function to generate all unique partitions of an integer
  46.     static void printAllUniqueParts(int n, int teamSize)
  47.     {
  48.         n = n * (n - 1) / 2;
  49.         int[] p = new int[n]; // An array to store a partition
  50.         int k = 0;  // Index of last element in a partition
  51.         p[k] = n;  // Initialize first partition as number itself
  52.  
  53.         // This loop first prints current partition, then generates next
  54.         // partition. The loop stops when the current partition has all 1s
  55.         while (true)
  56.         {
  57.             // print current partition
  58.             printArray(p, k+1, teamSize);
  59.  
  60.             // Generate next partition
  61.  
  62.             // Find the rightmost non-one value in p[]. Also, update the
  63.             // rem_val so that we know how much value can be accommodated
  64.             int rem_val = 0;
  65.             while (k >= 0 && p[k] == 1)
  66.             {
  67.                 rem_val += p[k];
  68.                 k--;
  69.             }
  70.  
  71.             // if k < 0, all the values are 1 so there are no more partitions
  72.             if (k < 0)  return;
  73.  
  74.             // Decrease the p[k] found above and adjust the rem_val
  75.             p[k]--;
  76.             rem_val++;
  77.  
  78.  
  79.             // If rem_val is more, then the sorted order is violeted.  Divide
  80.             // rem_val in differnt values of size p[k] and copy these values at
  81.             // different positions after p[k]
  82.             while (rem_val > p[k])
  83.             {
  84.                 p[k+1] = p[k];
  85.                 rem_val = rem_val - p[k];
  86.                 k++;
  87.             }
  88.  
  89.             // Copy rem_val to next position and increment position
  90.             p[k+1] = rem_val;
  91.             k++;
  92.         }
  93.     }
  94.      
  95.     public static void main (String[] args)
  96.     {
  97.         outcomes = 0;
  98.         printAllUniqueParts(5, 5);
  99.         System.out.println("Number of unique outcomes:" + outcomes);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement