cjerian
BAN USERThis seems to be what you are trying to do, which does recursion to try out the cases and counts them.
package simple;
public class DFLSeq {
public int numSeq(int ones, int twos, int threes, int fours){
System.out.println("numSeq " + ones + " " + twos + " " + threes + " " +fours);
return numSeqHelper(ones, twos, threes, fours, 0) ;
}
public static void main(String[] args) {
int[] n = {4, 1, 1, 1};
DFLSeq d = new DFLSeq();
System.out.println( d.numSeq(n[0],n[1],n[2],n[3]));
}
public int numSeqHelper(int ones, int twos, int threes, int fours, int last){
if(ones ==0 && twos ==0 && threes ==0 && fours ==0)
return 1;
System.out.println("" + ones + " " + twos + " " + threes + " " +fours + " last=" +last);
int onesSeq = 0;
int twosSeq = 0;
int threesSeq = 0;
int foursSeq = 0;
if(last!= 1 && ones != 0)
onesSeq = numSeqHelper(ones-1, twos, threes, fours, 1);
if(last!= 2 && twos != 0)
twosSeq = numSeqHelper(ones, twos-1, threes, fours, 2);
if(last!= 3 && threes != 0)
threesSeq = numSeqHelper(ones, twos, threes-1, fours, 3);
if(last!= 4 && fours != 0)
foursSeq = numSeqHelper(ones, twos, threes, fours-1, 4);
int res= onesSeq +twosSeq+ threesSeq + foursSeq;
System.out.println("res="+res);
return res;
}
}
RepViennaJones, Reporter at Precious Moments
Experienced project manager and capable supervisor of a team of writers, reporters, illustrators, graphic artists, and other staff members. I ...
This seems to print the first case out that works, or prints out -1 if nothing works, but it doesn't count the number of working cases, it stops when it gets a res that is not -1 Now 5,1,1,1, has only 0 solutions so it returns -1, but 4,1,1,1 has 6 solutions, but this only finds the very first one. 1212134
- cjerian March 26, 2015