Skip to content

Instantly share code, notes, and snippets.

@robshep
Created June 26, 2013 15:48
Show Gist options
  • Save robshep/5868576 to your computer and use it in GitHub Desktop.
Save robshep/5868576 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Partition {
public static void partition(int n, int l) {
partition(n, n, new ArrayList<Integer>(), l);
}
public static void partition(int n, int max, ArrayList<Integer> prefix, int l) {
if (n == 0 && prefix.size() == l) {
System.out.print(prefix);
int score = 0;
for(Integer p : prefix)
{
score += Math.pow( Math.pow(2,(5-p)), (5-p));
}
System.out.print(" ");
System.out.println(score);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
ArrayList<Integer> newPrefix = new ArrayList<Integer>(prefix);
newPrefix.add(i);
partition(n-i, i, newPrefix, l);
}
count++;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
final int L = Integer.parseInt(args[1]);
final int MAX_F = 5;
if( N > L * (MAX_F)){
System.out.println( new ArrayList<Integer>() {{ for(int i=0;i<L;i++) { add(5); } }} );
return;
}
partition(N, MAX_F, new ArrayList<Integer>(), L);
System.out.println("Count: " + count);
}
static int count = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment